1、簡(jiǎn)介
蘋果爸爸在iOS 10.3的API來開放了更換icon的方法,客觀來說凉唐,實(shí)用性并不硬霸。
核心API是下面這個(gè)方法:
Objc
- (void)setAlternateIconName:(NSString *)alternateIconName
completionHandler:(void (^)(NSError *error))completionHandler
Swift 3.0
func setAlternateIconName(_ alternateIconName: String?,
completionHandler: ((Error?) -> Void)? = nil)
方法一目了然霍骄,設(shè)置替換圖標(biāo)名稱 & 回調(diào)
官方API :傳送門
按照官方文檔的解釋台囱,我們需要在info.plist里來一點(diǎn)操作,具體的key代表了什么读整,建議大家沒事就去 官方文檔 cmd+F+keyword搜出來啃一啃簿训。
2、使用
開干之前米间,先來一下英文考察:
Alternate:交替的强品、輪流的
Primary:主要的;最早的,原始的
-
在老地方放上初始化icon 在這里按照API叫 Primary Icon更好
檢查Primary Icon 命名屈糊,盡量保持規(guī)則的榛,后面有用0.0。
- 導(dǎo)入Alternate Icon,命名盡量規(guī)則逻锐。Mark:資源直接放到Bundle夫晌,目前放到Assets并不能愉快的玩耍
- 在Info.plist中添加響應(yīng)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
- 調(diào)用最上頭簡(jiǎn)介里的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
終于找到了一個(gè)正常的GIF制作工具昧诱,Demo傳送門,感謝翻閱晓淀。