iOS开发动态更换app图标

1. 导入图片.图片放在Assets.xcassets 里不好使.放工程里

2.配置.plist如下

3.代码:

- (void)change

{

    if (![[UIApplication sharedApplication] supportsAlternateIcons]) {

        NSLog(@"retrun");

        return;

    }

    

    NSString *iconName = [NSString stringWithFormat:@"%d",self.num];

    NSLog(@"%@",iconName);

    [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {

        if (error) {

            NSLog(@"更换app图标发生错误了 : %@",error);

        } else {

            NSLog(@"更换app-success");

        }

    }];

    self.num += 1;

    if (self.num == 6) {

        self.num = 3;

    }

}

 

4.更换的时候,系统方法会有弹框.重新系统方法代码如下:

//
//  UIViewController+Present.h
//  IconTest
//
//  Created by 司文 on 2019/8/7.
//  Copyright © 2019 司文. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (Present)

@end

NS_ASSUME_NONNULL_END
//
//  UIViewController+Present.m
//  IconTest
//
//  Created by 司文 on 2019/8/7.
//  Copyright © 2019 司文. All rights reserved.
//

#import "UIViewController+Present.h"
#import <objc/runtime.h>

@implementation UIViewController (Present)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
        Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(dy_presentViewController:animated:completion:));
        // 交换方法实现
        method_exchangeImplementations(presentM, presentSwizzlingM);
    });
}
- (void)dy_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    
//    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
        NSLog(@"title : %@",((UIAlertController *)viewControllerToPresent).title);
        NSLog(@"message : %@",((UIAlertController *)viewControllerToPresent).message);
//        
//        //这里只是判断了 如果UIAlertController 中title 和message 都没有信息 那么返回空,这样就不会弹框,当然你可以判断你需要的。
//        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
//        if (alertController.title == nil && alertController.message == nil) {
            NSLog(@"iconName = %@",[[UIApplication sharedApplication] alternateIconName]);
//            NSLog(@"IsListening: %@\nCallStacks:\n%@", [[UIApplication sharedApplication] alternateIconName], NSThread.callStackSymbols);
//
//            return;
//        } else {
//            [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
//            return;
//        }
//    }
//    
//    [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
    
}


@end

以上参考.胖胖大神博客:https://blog.csdn.net/siwen1990

以下正逢七夕,用这个做个表白小惊喜喽.如图 可以做成某某某我爱你

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值