聲明:
轉(zhuǎn)載請(qǐng)注明出處:http://www.reibang.com/p/9c78f161eeb8
評(píng)論請(qǐng)到原文,轉(zhuǎn)載一概不回復(fù)
iOS動(dòng)態(tài)更換App圖標(biāo)方法就不說了杜恰,這里給一篇詳細(xì)的介紹文章:iOS動(dòng)態(tài)更換App圖標(biāo)(一):基礎(chǔ)使用
這里只說一下遇到的坑
坑1
setAlternateIconName
不能在didFinishLaunchingWithOptions
中調(diào)用贫母,會(huì)報(bào)3072錯(cuò)誤洞就,需要在ViewController
中調(diào)用卵皂,初步猜想可能是需要window
初始化成功后調(diào)用悼粮,需要一個(gè)ViewController
或者是UINavigationController
之類的闲勺,沒有去驗(yàn)證
坑2
supportsAlternateIcons
只是檢測(cè)是否能進(jìn)行更換,并不能進(jìn)行系統(tǒng)版本號(hào)判斷扣猫,需要自己進(jìn)行版本判斷菜循,要不然在低版本系統(tǒng)中會(huì)崩潰,比如:iOS7
坑3
icon資源文件需要在項(xiàng)目目錄下申尤,不能是Assets.xcassets
中的圖片癌幕,否則無效
技巧點(diǎn)1
動(dòng)態(tài)更換App圖標(biāo)會(huì)有彈框衙耕,這個(gè)可以去掉,教程:iOS動(dòng)態(tài)更換App圖標(biāo)(二):無彈框更換App圖標(biāo)
其實(shí)不用非要使用擴(kuò)展勺远,寫在調(diào)用setAlternateIconName
的ViewController
里面就可以了橙喘,在調(diào)用setAlternateIconName
前使用代碼
Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(dy_presentViewController:animated:completion:));
// 交換方法實(shí)現(xiàn)
method_exchangeImplementations(presentM, presentSwizzlingM);
比如:
- (void)viewDidLoad {
[super viewDidLoad];
Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(dy_presentViewController:animated:completion:));
// 交換方法實(shí)現(xiàn)
method_exchangeImplementations(presentM, presentSwizzlingM);
NSString *alternateIconName = [[UIApplication sharedApplication] alternateIconName];
if (alternateIconName) {
//重置到默認(rèn)icon
[self setAppIconWithName:nil];
} else {
NSString *IconName = @"AppIcon-1";
[self setAppIconWithName:IconName];
}
}
- (void)setAppIconWithName:(NSString *)iconName {
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ā)生錯(cuò)誤了:\n%@",error);
}
}];
}
- (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];
}
技巧點(diǎn)2
plist
的CFBundleIcons
中不需要設(shè)置默認(rèn)圖片,只需要設(shè)置替換的圖片就可以了胶逢,UIPrerenderedIcon
也不需要設(shè)置厅瞎,這個(gè)是iOS7之前的參數(shù)(如果你現(xiàn)在還在支持iOS6,我只能說上帝保佑你宪塔,可憐的孩子磁奖!)囊拜,比如:
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>AppIcon-1</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon-1</string>
</array>
</dict>
</dict>
</dict>
使用的時(shí)候參考我上面提供的代碼就行了
我只是粗略測(cè)試了一下某筐,如果有出錯(cuò)的地方謝謝大家指出來
另外如果想在編譯的時(shí)候就把圖標(biāo)替換了,可以使用腳本來做冠跷,這樣就不能在運(yùn)行時(shí)替換了
--------------------6.7
附上jenkins打包替換圖標(biāo)方法南誊,沒寫過jenkins腳本的就要看了,我寫的比較粗略