您的位置:首页 > 移动开发

iOS 10.3新特性之动态替换App Icon

2017-03-31 09:32 423 查看

动态更换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");
}];
}


一个特别奇怪的新特性,鬼知道apple要干嘛,难不成以后手机里面清一色一个icon,这样看起来也是很装B的。。。。。。

Demo地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息