我自己使用的方法:
- (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
网友评论