IOS development] iOS 10.3 how to replace the app Icon

IOS 10.3 open to replace the app icon API, the core method is the following:

Func setAlternateIconName (alternateIconName: String, completionHandler: (_? (Error?) -> Void = Nil)?)

This is the official document, but you still need to fill in the info.plist inside something to make it work, this part of the official comment here.

IOS development] iOS 10.3 how to replace the app Icon

But info.plist how to fill out this part of the reading is still somewhat obscure, may not know how to operate, the following demonstration.

IOS development] iOS 10.3 how to replace the app Icon
Assets.xcassets
IOS development] iOS 10.3 how to replace the app Icon
info.plist
< key> CFBundleIcons< /key> < dict> < key> CFBundleAlternateIcons< /key> < dict> < key> blackBgColor< /key> < dict> < key> CFBundleIconFiles< /key> < array& gt; < string> blackBgColor< /string> < /array> < key> UIPrerenderedIcon< /key> < false/> < /dict> < /dict> < key> CFBundlePrimaryIcon< /key> < dict> < key> CFBundleIconFiles< /key> < array> < string> AppIcon60x60< /string> < /array> < /dict> < /dict>

Figure Primary, Icon field to write to AppIcon60x60 because here xcassets I only import [email protected] and [email protected] images, here is 60 for iPhone, 60pt icon picture resources required for the highest quality, lower resolution version of the system will be automatically compressed to display.

BlackBgColor is my picture resource for replacing native icons. The name of the file needs to be consistent with the info.plist (note that info.plist used the two “blackBgColor”), and this is when you set the icon in the code, you need to give API parameters. The same is the [email protected] and [email protected] image resources, the file is not added to the Assets.xcassets to come in, but directly into the directory.

If you need to support iPad, it is recommended to use 83.5pt (iPad Pro) image resources. In addition, some other about replacing the icon in the iPad of the note, is described here, note that we used here in info.plist key is CFBundleIcons, another is key CFBundleIcons~ipad.

Replace the icon part of the code is super simple:

Import UIKit class ViewController: UIViewController override func (viewDidLoad) {super.viewDidLoad} {(@IBAction) func changeAppIcon (_ sender: Any) {if {print ("UIApplication.shared.supportsAlternateIcons you can change this app's icon}else) {print (" you cannot change this app's icon return if let name}) = UIApplication.shared.alternateIconName {/ / CHANGE TO PRIMARY ICON UIApplication.shared.setAlternateIconName (NIL) {(err:Error? In) print ("set icon error:/ (String (describing: ERR)) print (" the ")} alternate icon's name (name) is /}else") {/ / CHANGE TO ALT ERNATE ICON UIApplication.shared.setAlternateIconName ("blackBgColor") {(err:Error? In) print ("set icon error:/ (String (describing: ERR))}}}}")

This is the full demo address for all of the above

IOS development] iOS 10.3 how to replace the app Icon
screentshot0
IOS development] iOS 10.3 how to replace the app Icon
screentshot1