背景:
在iOS10.3之前如果想替换APP的图标,必须通过Assets.xcassets添加AppIcon,而且是唯一指定的,不能够修改。
iOS10.3之后,系统提供了修改AppIcon的API,可以通过内置几个不同的icon,然后通过代码设置不同的AppIcon。
核心API:
// Pass `nil` to use the primary application icon. The completion handler will be invoked asynchronously on an arbitrary background queue; be sure to dispatch back to the main queue before doing any further UI work.
//如果name设置为nil,会自动读取主app icon
- (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));
官方文档:
info.plist具体配置:
Primary Icon 字段设置为AppIcon(就是asset里面的名字),如果设置appicon的API的name参数设置为nil,会自动读取Primary Icon对应的图片,
Test1是用来替换原生图标的图片资源,文件名需要和info.plist中保持一致,需要注意的是 图片资源需要放在外边,不能放在asset资源里面。
具体实现代码
- (IBAction)testClick:(id)sender {
[[UIApplication sharedApplication] setAlternateIconName:@"Test1" completionHandler:^(NSError * _Nullable error) {
NSLog(@"error = %@",error.localizedDescription);
}];
}
- (IBAction)test2Click:(id)sender {
[[UIApplication sharedApplication] setAlternateIconName:@"Test2" completionHandler:^(NSError * _Nullable error) {
NSLog(@"error = %@",error.localizedDescription);
}];
}
- (IBAction)emptyClick:(id)sender {
[[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
NSLog(@"error = %@",error.localizedDescription);
}];
}
实现效果:
修改前:
点击按钮修改:
修改后: