iOS 10.3 开放了更换 app 图标的 API,核心方法是下面这个:
func setAlternateIconName(_ alternateIconName: String?,
completionHandler: ((Error?) -> Void)? = nil)
详情可以参考官方文档,如果你想要实现更换APP图标的功能,你还需要在 info.plist 添加一些参数才行,参数可参考官方注释。
info.plist
把你想替换的图标名称添加到配置文件中(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 中用到了两次 图片名称
代码部分
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(supportsAlternateIcons)]) {
NSString *name = application.alternateIconName;
if (nil == name) {
if (nil == actionLabel) {
[UIAlertView bk_showAlertViewWithTitle:@"请选择一个图标" message:nil cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
else{
NSString *imageName = actionLabel.text;//获取用户选取的图标名称(必须和配置文件中的名称一致),如果只有一个,也可以写死
//替换图标
[application setAlternateIconName:imageName completionHandler:^(NSError * _Nullable error) {
if (error) {
[UIAlertView bk_showAlertViewWithTitle:@"更换图标失败" message:error.domain cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
}];
}
}
else{
//更新为原来的图标
[application setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
if (error) {
[UIAlertView bk_showAlertViewWithTitle:@"更换图标失败" message:error.domain cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}
}];
}
}
else{
[UIAlertView bk_showAlertViewWithTitle:@"不能更换图标" message:@"此功能仅限10.3及以上系统" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
}];
}