最近iOS10.3更新了,然后有个好玩的功能,就是可以更新应用图标了。
API文档
下面来看看最近的iOS10.3关于icon的API
@interface UIApplication (UIAlternateApplicationIcons)
// If false, alternate icons are not supported for the current process.
@property (readonly, nonatomic) BOOL supportsAlternateIcons NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));
// 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.
- (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));
// If `nil`, the primary application icon is being used.
@property (nullable, readonly, nonatomic) NSString *alternateIconName NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));
@end
实现方案
上代码:
首先得判断系统是否支持更换图标(文档中有一个BOOL的属性,就是判断是否支持的)
[UIApplication sharedApplication].supportsAlternateIcons
然后在做图标的更换。
if ([UIApplication sharedApplication].alternateIconName) {
[[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
NSLog(@"the alternate icon's error is:%@", error.description);
}];
} else {
[[UIApplication sharedApplication] setAlternateIconName:@"blackBgColor" completionHandler:^(NSError * _Nullable error) {
NSLog(@"the alternate icon's error is:%@", error);
}];
}
其中传入的nil,默认代表的是最初始的那张图片(文档中有相关的解释)。
运行报错时
运行的时候,点击切换按钮,通过打印的error的日志得到的是CFBundleAlternateIcons的缺失,所以需要在plist文件中进行设置。
plist文件的配置
网友评论