iOS 10.3 開放了更換 app 圖標(biāo)的 API,核心方法是下面這個:
func setAlternateIconName(_ alternateIconName: String?,
completionHandler: ((Error?) -> Void)? = nil)
詳情可以參考官方文檔,如果你想要實現(xiàn)更換APP圖標(biāo)的功能,你還需要在 info.plist 添加一些參數(shù)才行,參數(shù)可參考官方注釋。
info.plist
把你想替換的圖標(biāo)名稱添加到配置文件中(PS:可以為多個,此處示例只寫兩個)
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>流程_2</key>
<dict>
<key>UIPrerenderedIcon</key>
<false/>
<key>CFBundleIconFiles</key>
<array>
<string>流程_2</string>
</array>
</dict>
<key>流程_1</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>流程_1</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
</dict>
</plist>
注意 info.plist 中用到了兩次 圖片名稱

INFO.png
代碼部分
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(supportsAlternateIcons)]) {
NSString *name = application.alternateIconName;
if (nil == name) {
if (nil == actionLabel) {
[UIAlertView bk_showAlertViewWithTitle:@"請選擇一個圖標(biāo)" message:nil cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
else{
NSString *imageName = actionLabel.text;//獲取用戶選取的圖標(biāo)名稱(必須和配置文件中的名稱一致),如果只有一個,也可以寫死
//替換圖標(biāo)
[application setAlternateIconName:imageName completionHandler:^(NSError * _Nullable error) {
if (error) {
[UIAlertView bk_showAlertViewWithTitle:@"更換圖標(biāo)失敗" message:error.domain cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
}];
}
}
else{
//更新為原來的圖標(biāo)
[application setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
if (error) {
[UIAlertView bk_showAlertViewWithTitle:@"更換圖標(biāo)失敗" message:error.domain cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
}];
}
}
else{
[UIAlertView bk_showAlertViewWithTitle:@"不能更換圖標(biāo)" message:@"此功能僅限10.3及以上系統(tǒng)" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
效果

Simulator Screen Shot 2017年4月1日 11.03.51.png