前几天Apple发布了iOS10.3的更新包,这个更新包带来了很多新的特性,特意去研究了一下。
无奈自己的拖延症,现在才发出来。先附上Demo地址。
其实这个已经有App第一时间做了这个功能,并且上线了,大家可以去AppStore搜索一个App:MLB.com At Bat,观看一下这个App的介绍视频,就会知道这个是什么意思了。
这是官方文档,但是你还需要在 info.plist 里面填一些东西才能让它起作用,这部分官方注释内容在这里。可以查看一下table5。
需要在plist文件里面添加如下代码:
<key>CFBundleIcons</key>
<dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>blackIcon</string>
</array>
</dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>blueIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>blueIcon</string>
</array>
</dict>
<key>blackIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>blackIcon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>redIcon</key>
<dict>
<key>UIPrerenderedIcon</key>
<false/>
<key>CFBundleIconFiles</key>
<array>
<string>redIcon</string>
</array>
</dict>
</dict>
</dict>
其中icon要对应你的图标的名字
其中有一个key UIPrerenderedIcon
这个一开始我也不知道是做什么的,但是后来看了官方文档,发现这个就是在之前的iOS版本用来给图标添加高光用的,其实这个在SB中也有设置的地方
我试了,不添加这个键也不会有任何影响。
好了准备工作做好了就要添加修改图标的代码了
- (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));```
[[UIApplication sharedApplication] setAlternateIconName:IconName completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"成功更换为%@",IconName);
}else{
NSLog(@"error:%@",error);
}
}];
具体大家可以看我上面的Demo,非常易懂。设置为nil的时候就是还原原来的图标。
如果大家想看swift版本的和ipad的可以移步。
参考文档http://www.tuicool.com/articles/miuAfuJ
https://forums.developer.apple.com/thread/71463