从iOS10.3开始,app可以动态的更换桌面图标,具体方法是
func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Void)? = nil)文档链接 https://developer.apple.com/reference/uikit/uiapplication/2806818-setalternateiconname
效果图如下:
顺便附上我源码链接:https://github.com/wddyzzw/ChangeAppIcon.git
根据官方给的文档上面写的,需要在info.plist文件配置CFBundleAlternateIcons和CFBundlePrimaryIcon,他们都属于CFBundleIcons的子项。
alternateIconName The name of the alternate icon, as declared in the CFBundleAlternateIcons key of your app's Info.plist file. Specify nil if you want to display the app's primary icon, which you declare using the CFBundlePrimaryIcon key. Both keys are subentries of the CFBundleIcons key in your app's Info.plist file.下面是我的info.plist的截图
//这里是具体的代码 <key>CFBundleIcons</key> <dict> <key>CFBundleAlternateIcons</key> <dict> <key>DownloadAppIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string>DownloadAppIcon</string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> </dict> <key>CFBundlePrimaryIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string>AppIcon60x60</string> </array> </dict> </dict>其中那个AppIcon60x60指的是原始的app图标,这个固定这样写就行。
具体代码实现如下:
//这里是点击新icon的方法 @IBAction func changeNewIcon(_ sender: Any) { if application.supportsAlternateIcons { //判断是否支持备用图标 application.setAlternateIconName("DownloadAppIcon") { (error) in print(error?.localizedDescription ?? "") } } } //这里是点击旧icon的方法 @IBAction func changeOldIcon(_ sender: Any) { if application.supportsAlternateIcons { //当备用图标为nil时,表示使用原始app图标 application.setAlternateIconName(nil) { (error) in print(error?.localizedDescription ?? "") } } }这样就完成了在iOS10.3及以上的iPhone上更换app图标。