1、简介
苹果爸爸在iOS 10.3的API来开放了更换icon的方法,客观来说,实用性并不硬霸。
核心API是下面这个方法:
Objc
- (void)setAlternateIconName:(NSString *)alternateIconName
completionHandler:(void (^)(NSError *error))completionHandler
Swift 3.0
func setAlternateIconName(_ alternateIconName: String?,
completionHandler: ((Error?) -> Void)? = nil)
方法一目了然,设置替换图标名称 & 回调
官方API :传送门
按照官方文档的解释,我们需要在info.plist里来一点操作,具体的key代表了什么,建议大家没事就去 官方文档 cmd+F+keyword搜出来啃一啃。
2、使用
开干之前,先来一下英文考察:
Alternate:交替的、轮流的
Primary:主要的;最早的,原始的
-
在老地方放上初始化icon 在这里按照API叫 Primary Icon更好
检查Primary Icon 命名,尽量保持规则,后面有用0.0。
- 导入Alternate Icon,命名尽量规则。Mark:资源直接放到Bundle,目前放到Assets并不能愉快的玩耍
- 在Info.plist中添加响应key-value,Pay attention here,budy
- Open as source code
<plist version="1.0">
<dict>
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>UIPrerenderedIcon</key>
<false/>
<key>icontwo</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>icontwo</string>
</array>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>Icon60x60</string>
</array>
</dict>
</dict>
- Focus on picture
- 调用最上头简介里的API,查看效果
Objc
- (void)changeIcon{
if ([UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"this app can change app icon");
}else{
NSLog(@"sorry,this app can not change app icon");
return;
}
NSString *iconName = [[UIApplication sharedApplication] alternateIconName];
if (iconName) {
[[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"set icon error: %@",error);
}
NSLog(@"current icon's name -> %@",iconName);
}];
}else{
[[UIApplication sharedApplication] setAlternateIconName:@"icontwo" completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"set icon error: %@",error);
}
NSLog(@"current icon's name -> %@",iconName);
}];
}
}
Swift3.0
func changeIcon() {
if UIApplication.shared.supportsAlternateIcons {
print("this app can change app icon")
}
else {
print("sorry,this app can not change app icon")
return
}
let iconName: String = UIApplication.shared.alternateIconName!
if iconName != "" {
UIApplication.shared.setAlternateIconName(nil, completionHandler: {(_ error: Error?) -> Void in
if error != nil {
print("set icon error: \(String(describing: error))")
}
print("current icon's name -> \(iconName)")
})
}
else {
UIApplication.shared.setAlternateIconName("icontwo", completionHandler: {(_ error: Error?) -> Void in
if error != nil {
print("set icon error: \(String(describing: error))")
}
print("current icon's name -> \(iconName)")
})
}
}
- Game over
终于找到了一个正常的GIF制作工具,Demo传送门,感谢翻阅。