第一種方式:
一家制、info.plist添加key
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>Icon1</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>app_icon_tx</string>
</array>
</dict>
<key>Icon2</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>app_icon_jd</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array/>
</dict>
</dict>
顯示為:
CFBundlePrimaryIcon 對應(yīng)的就是默認(rèn)的圖標(biāo),默認(rèn)圖標(biāo)可以不寫,在Asset里面會有設(shè)置, 而Icon1鼻忠、Icon2 就是我們的使用到的動(dòng)態(tài)更換的圖標(biāo),將動(dòng)態(tài)更換的圖標(biāo)不能放置在asset中, 需要放到項(xiàng)目中以文件的形式保存.
二写妥、動(dòng)態(tài)更換圖標(biāo)
改API是iOS10.3以后的版本支持,低于該版本需要加判斷
- (void)replaceAppIconWithIconName:(NSString *)iconName {
UIApplication *application = [UIApplication sharedApplication];
//判斷系統(tǒng)是否支持icon的切換
if ([application supportsAlternateIcons]) {
//執(zhí)行切換icon
[application setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"error==> %@",error.localizedDescription);
}else{
NSLog(@"done!!!");
}
}];
}
}
iconName 就是動(dòng)態(tài)更換圖標(biāo)的的名稱, 也就是我們在info.plist文件中制定的名稱,Icon1雳灵、Icon2迎吵;
如果想切換到默認(rèn)的圖標(biāo)如何操作呢? iconName設(shè)置為nil, 然后調(diào)用這個(gè)函數(shù)就可以實(shí)現(xiàn)了.
但是躲撰,如果這樣更新圖標(biāo),會有彈框提示用戶已經(jīng)更換了圖標(biāo)击费,我們更需要的是無感更換拢蛋;
三、無彈框更換
無彈框更換蔫巩,需要使用runtime谆棱,黑魔法hook系統(tǒng)的圖框方法,進(jìn)行攔截圆仔;
#import <objc/runtime.h>
@implementation UIViewController
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method originalPresentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
Method swizzlingPresentM = class_getInstanceMethod(self.class, @selector(riffle_presentViewController:animated:completion:));
if (!class_addMethod([self class], @selector(riffle_presentViewController:animated:completion:), method_getImplementation(swizzlingPresentM), method_getTypeEncoding(swizzlingPresentM))) {
method_exchangeImplementations(originalPresentM, swizzlingPresentM);
}
});
}
- (void)riffle_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
if (alertController.title == nil && alertController.message == nil) {
return;
} else {
[self riffle_presentViewController:viewControllerToPresent animated:flag completion:completion];
return;
}
}
[self riffle_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
@end
第二種方式:
xcode13及以后版本支持
-
向Assets.xcassets內(nèi)添加一組新的ICON:
2.在項(xiàng)目設(shè)置里的允許使用多套ICON:
選擇項(xiàng)目->Build Setting->搜索Include all app icon assets垃瞧,然后改為YES:
更換App Iocn的方法是一樣的:
-(void)newReplaceAppIconWithIconName:(NSString *)iconName{
UIApplication *application = [UIApplication sharedApplication];
//判斷系統(tǒng)是否支持icon的切換
if ([application supportsAlternateIcons]) {
//執(zhí)行切換icon
[application setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"set alternative icon error:%@", error.localizedDescription);
}
}];
}
}
多套icon是需要蘋果審核后才可以在蘋果后臺進(jìn)行切換。
可以參考蘋果官方文檔:配置處理方案 - App Store Connect 幫助