First, set the icon information in the Info.plist
First, the icons that need to be replaced are declared in the following way so that we can call the files and methods normally. Note that the icon name and the corresponding file name of each icon correspond to each other.
Second, add the icon file in the project root directory
The icon file needs to be added under the root directory for normal invocation. Note that the icons are available in 2x and 3x sizes of two 120x120 and 180x180 respectively.
Of course Assets
also to add, in order to facilitate the use of preview in the application.
Iii. Call the replacement function where it is used
The final step is actually very simple, in the need to call the place changeIcon(iconName:String?)
, parameters for us to add a good app icon name. In the changeIcon(iconName:String?)
need to determine whether to support the replacement of icons, so as to avoid errors, of course, can also be judged in advance to show or hide the feature.
func changeIcon(iconName:String?) { if #available(iOS 10.3, *) { if UIApplication.shared.supportsAlternateIcons {//判断设备是否支持更换图标 print("支持更换图标!") //开始更换 UIApplication.shared.setAlternateIconName(iconName, completionHandler: { (error) in if error != nil { print("替换icon失败\(String(describing: error?.localizedDescription))") } }) }else { print("设备不支持更换图标") } } else { // Fallback on earlier versions } }
https://github.com/bjdehang/OneSwift/blob/master/5.%E5%A6%82%E4%BD%95%E5%9C%A8%E5%BA%94%E7%94%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E5%9B%BE%E6%A0%87%E6%9B%B4%E6%8D%A2%E5%8A%9F%E8%83%BD.md
How iOS adds icon replacement in your app