首先需要注意的是瓜饥,動(dòng)態(tài)更換圖標(biāo)需要在iOS10.3之后才可以使用
第一步浴骂、需要在info.plist中配置圖標(biāo)的一些信息,具體如下
如下為info.plist中的配置趣苏,拿走不謝
<key>CFBundleIcons</key> <dict> <key>CFBundleAlternateIcons</key> <dict> <key>10.1</key> <dict> <key>UIPrerenderedIcon</key> <false/> <key>CFBundleIconFiles</key> <array> <string>Icon-29</string> <string>Icon-60</string> <string>Icon-Spotlight-40</string> <string>icon-1024</string> </array> </dict> <key>7.1</key> <dict> <key>UIPrerenderedIcon</key> <false/> <key>CFBundleIconFiles</key> <array> <string></string> </array> </dict> </dict> <key>CFBundlePrimaryIcon</key> <dict> <key>CFBundleIconName</key> <string></string> <key>CFBundleIconFiles</key> <array> <string></string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> <key>UINewsstandIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string></string> </array> <key>UINewsstandBindingType</key> <string>UINewsstandBindingTypeMagazine</string> <key>UINewsstandBindingEdge</key> <string>UINewsstandBindingEdgeLeft</string> </dict> </dict>
第二步食磕、需要執(zhí)行代碼去進(jìn)行更換圖標(biāo)的操作,這種情況下會(huì)有彈框提示更換彬伦,點(diǎn)擊確定會(huì)進(jìn)行更換圖標(biāo)操作
- (void)setAppIconWithName:(NSString*)iconName {
? ? if(@available(iOS10.3, *)) {
? ? ? ? if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
? ? ? ? ? ? return;
? ? ? ? }
? ? }else{
? ? ? ? // Fallback on earlier versions
? ? }
? ? if([iconNameisEqualToString:@""]) {
? ? ? ? iconName =nil;
? ? }
? ? if(@available(iOS10.3, *)) {
? ? ? ? [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
? ? ? ? ? ? if(error) {
? ? ? ? ? ? ? ? NSLog(@"更換app圖標(biāo)發(fā)生錯(cuò)誤了 : %@",error);
? ? ? ? ? ? }
? ? ? ? }];
? ? }else{
? ? ? ? // Fallback on earlier versions
? ? }
}
第三步单绑、優(yōu)化彈框,在用戶無感知的情況下進(jìn)行圖標(biāo)切換搂橙,需要通過runtime來處理此操作歉提,寫一個(gè)UIViewController擴(kuò)展類,具體處理如下
+ (void)load{
? ? staticdispatch_once_tonceToken;
? ? dispatch_once(&onceToken, ^{
? ? ? ? MethodpresentM =class_getInstanceMethod(self.class,@selector(presentViewController:animated:completion:));
? ? ? ? MethodpresentSwizzlingM =class_getInstanceMethod(self.class,@selector(dy_presentViewController:animated:completion:));
? ? ? ? method_exchangeImplementations(presentM, presentSwizzlingM);
? ? });
}
- (void)dy_presentViewController:(UIViewController*)viewControllerToPresentanimated:(BOOL)flagcompletion:(void(^)(void))completion {
? ? if([viewControllerToPresentisKindOfClass:[UIAlertControllerclass]]) {
? ? ? ? UIAlertController*alertController = (UIAlertController*)viewControllerToPresent;
? ? ? ? if(alertController.title==nil&& alertController.message==nil) {
? ? ? ? ? ? return;
? ? ? ? }else{
? ? ? ? ? ? [selfdy_presentViewController:viewControllerToPresentanimated:flagcompletion:completion];
? ? ? ? ? ? return;
? ? ? ? }
? ? }
? ? [selfdy_presentViewController:viewControllerToPresentanimated:flagcompletion:completion];
}
到這里完整的更換圖標(biāo)就可以了,親測可用