动态更换Icon
先看下iOS 10.3下新增的这三个属性
@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
第一个
supportsAlternateIcons
是否支持动态替换
第二个
也就是核心方法,根据配置,替换自己想要的icon,具体参数描述以及介绍大家基本上都知道,按下option进去看看就行了,主要还是涉及到info.plist的配置
官方指定的plist key介绍
第三个
替换icon的时候根据该属性获取到当前被替换到显示的icon名称,例如你没换的时候就是nil,换了icon1,那么就显示icon1
Plist配置以及Icon图片如何存放
第一步—>设置plist
首先看下info.plist,写的很详细了,依葫芦画瓢总会吧
第二步—>放iCON
首先和一个icon一样,也就是对应的Primary Icon,直接拖进Asset里面管理,需要注意的是,需要轮流替换的其他icon不能拖进去啊,不然跑起来,error了,直接和文件一样建个文件夹放在目录下即可,具体的字段和配置看一眼Demo搞一次就明白了,这里讲再多都没用
第三步—>代码
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
NSLog(@"不支持。。。");
return;
}
else
{
NSLog(@"支持动态替换");
}
NSString *icon = [[UIApplication sharedApplication] alternateIconName];
if (icon) {
NSLog(@"icon is exist");
NSString *changeStr = nil;
if ([icon isEqualToString:@"IconChange"]) {
changeStr = @"IconChangeNext";
}
[[UIApplication sharedApplication] setAlternateIconName:changeStr completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"error");
}
NSLog(@"done");
}];
}
else
{
NSLog(@"icon not exist");
[[UIApplication sharedApplication] setAlternateIconName:@"IconChange" completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"error");
}
NSLog(@"done");
}];
}