iOS 動態(tài)設(shè)置AppIcon應(yīng)用圖標

前提是操作系統(tǒng)在10.3或以上才可以。

可以點擊 官方文檔 查看
主要是下面的這個方法:
- (void)setAlternateIconName:(NSString *)alternateIconName 
           completionHandler:(void (^)(NSError *error))completionHandler;
我們可以新建一個項目Demo
1.注意:將需要更改的應(yīng)用圖標在工程中新建一個目錄,不要放在Assets.xcassets這里面。如下圖:
change.png

我這里面只是個例子,你需要把尺寸都填全了?。。?/p>

2.接下來配置info.plist文件:也可以參考官方文檔: 鏈接點擊 配置如下圖:
123123.jpeg
解釋如下:
  • 在info.plist新建一個key::Icon files (iOS 5) 類型為字典。

  • 在 Icon files(iOS 5)內(nèi)添加一個Key: CFBundleAlternateIcons ,類型為字典。

  • 在這個字典里配置我們所有需要動態(tài)修改的應(yīng)用圖標。

  • 這里已圖片名為key,也是一個字典,里面是CFBundleIconFiles:類型是數(shù)組,添加圖片的各個尺寸。

  • UIPrerenderedIcon:為Boolean類型 為NO,也可以不設(shè)置。

  • 到這里基本的配置就完事了,接下來就是代碼的實現(xiàn)了!
代碼如下:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self changeIconAction];
    });
}
方法:
- (void)changeIconAction
{
    NSString *iconName = @"icon-60@2x";
    if (@available(iOS 10.3, *)) {
        [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"更換圖標失敗 == %@", error);
            }
            else
            {
                NSLog(@"更換成功");
            }
            }];
    } else {
        // Fallback on earlier versions
        NSLog(@"不支持");
    }
}
然后運行Demo,效果圖如下:
WechatIMG62.jpeg
  • 這個時候會彈出一個框,這是是系統(tǒng)彈出來的,提示你更改應(yīng)用圖標,一般我們不希望出現(xiàn)這個提示,我們可以使用RunTime動態(tài)替換方法去修改.
我們新建一個UIController的Category:代碼如下:
#import "UIViewController+Category.h"
#import <objc/runtime.h>

@implementation UIViewController (Category)

+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
        Method dismissAlertViewController = class_getInstanceMethod(self.class, @selector(dismissAlertViewControllerPresentViewController:animated:completion:));
        //runtime替換方法
        method_exchangeImplementations(presentM, dismissAlertViewController);
    });
}

- (void)dismissAlertViewControllerPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)animated completion:(void (^)(void))completion {
    
    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) {
            return;
        }
    }
    [self dismissAlertViewControllerPresentViewController:viewControllerToPresent animated:animated completion:completion];
}

@end

完成?。?!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容