在iOS10.3之前如果想替換APP的圖標(biāo),必須通過Assets.xcassets添加AppIcon,而且是唯一指定的,不能夠修改。
iOS10.3之后,系統(tǒng)提供了修改AppIcon的API,可以通過內(nèi)置幾個不同的icon,然后通過代碼設(shè)置不同的AppIcon。
準(zhǔn)備工作
首先拖入需要替換的Icon

image.png
然后修改info.plist

image.png
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>晴</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>晴</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>多云</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>多云</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>小雨</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>小雨</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>大雨</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>大雨</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>雪</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>雪</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<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>
核心代碼
NSArray *weathers = @[@"晴", @"多云", @"小雨", @"大雨", @"雪", @""];
NSString *iconName = weathers[arc4random() % weathers.count];
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
return;
}
if ([iconName isEqualToString:@""]) {
iconName = nil;
}
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"更換app圖標(biāo)發(fā)生錯誤了 : %@",error);
}
}];
不想彈窗,就用runtime替換調(diào)彈窗方法
+ (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 *alertController = (UIAlertController *)viewControllerToPresent;
if (alertController.title == nil && alertController.message == nil) {
return;
} else {
[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
return;
}
}
[self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
效果圖
彈窗

696588-03a2cedefec2fd40.gif
不彈窗

696588-09f00ce73de73194.gif