我自己使用的方法:
- (void)lc_setAlternateIconName:(NSString*)iconName
{
//anti apple private method call analyse
if ([[UIApplication sharedApplication] respondsToSelector:@selector(supportsAlternateIcons)] &&
[[UIApplication sharedApplication] supportsAlternateIcons])
{
NSMutableString *selectorString = [[NSMutableString alloc] initWithCapacity:40];
[selectorString appendString:@"_setAlternate"];
[selectorString appendString:@"IconName:"];
[selectorString appendString:@"completionHandler:"];
SEL selector = NSSelectorFromString(selectorString);
IMP imp = [[UIApplication sharedApplication] methodForSelector:selector];
void (*func)(id, SEL, id, id) = (void *)imp;
if (func)
{
func([UIApplication sharedApplication], selector, iconName, ^(NSError * _Nullable error) {});
}
}
}
注意事項
1.不要在Appdelegate.m中更換,如果要在Appdelegate.m更換則需要使用異步延時加載
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
xxxxxx // 更換圖標
});
2.icon資源文件需要在項目目錄下,不能是Assets.xcassets中的圖片,否則無效
3.supportsAlternateIcons只是檢測是否能進行更換,并不能進行系統版本號判斷,需要自己進行版本判斷,要不然在低版本系統中會崩潰,比如:iOS7