Original
Change icon for iOS app
Official
iOS10.3 has added an icon that allows developers to change the app, and then see how to change it.
The official API gives very little, but introduces an example method:
1 |
open func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Swift.Void)? = nil) |
According to the parameters passed in, we only need to pass in the alternate icon name, and then we can get the result of the change in the callback, the error is empty, and if it is unsuccessful, it will return the corresponding fault message (use error!. Localizeddescription to print view error messages). If it fails, the Alternateiconname property does not change. Note here:
If the icon used by the app is an alternate icon, then the value of this property is the name of the current icon, which is the name set in Info.plist, or nil if the current app shows the main (primary) icon.
Here are two points to note:
The system version of the current device. All of the APIs here are 10.3 to use.
Whether the current app supports alternate icon. Use the Supportsalertnateicons property to determine. Only the use of true can be changed.
The API also has a sentence that is more critical:
You must use Cfbundleicons in Info.plist to declare the primary and alternate icon for the current app. If you do not understand here, you can look down, first ignore.
Specific info.plist inside the field meaning here is not one by one instructions, details to understand here.
In short the official API above I only found so much. But to my feeling but a face confused, completely do not understand the following info.plist how to set. Therefore, the following preamble is available.
Personal understanding
In the Info.plist key introduction, we first look at other small knowledge.
We all know that there is a bundle display name in Info.plist, which sets the name of the app on the desktop. At this point, if we do not ignore this key, we continue to add cfbundledisplayname inside the Info.plist, you will receive such a hint:
1 |
The key you entered is already present in the dictionary. do you want to replace the existing key/value pair? |
But look at the key in the Info.plist dictionary, and there is no cfbundledisplayname, after the replacement found that it is the bundle display name. In other words, the key in Info.plist is not a key in Xcode, but the Xcode name. Such as:
This is the image from the official API, actually these keys have the corresponding Xcode name, that is, in the xcodes we can see the key. In fact, it is very simple, if you use the source form info.plist (right-click->open as->source Code), you will find that the key written here is listed above the key.
Let's look at the info.plist of the information I've set up online:
The cfbundleiconfiles here is the name of the alternate icon. The following primary icon is the default icon. The source code is:
Where cfbundleicons corresponds to icon files (IOS 5), Cfbundleiconfiles is primary icon.
Take a look at cfbundlealternateicons first. The value corresponding to this Cfbundlealternateicons key is a dictionary in iOS, for example:
The key of each dictionary is the name of the alternate icon, and this is what you pass into
1234567 |
IApplication.shared.setAlternateIconName(iconName) { (error)
in
if
(error != nil) {
self.aler(str: (error!.localizedDescription))
}
else
{
self.aler(str:
"修改成功"
)
}
}
|
Inside the iconname. The dictionary corresponding values are interpreted as follows:
Cfbundleiconfiles: This is a string array, each element is the name of the icon, you can add a number of different size of the icon to support Iphone,ipad.
Uiprerenderedicon: Specifies whether the app's icon contains a flash effect (shine effect), and if icon already has this effect, set this property to Yes to prevent the system from adding the same effect again. If set to No (the default), the iOS system will automatically add this effect. However, I did not test out this effect!!
Here also note: Primary Icon Item 0 name can not be filled out, Apple Official document also does not specifically say the name of how to fill out, just say if you want to use the Cfbundleprimaryicon key value defined icon, Direct use of the Setalternateiconname parameter Alternateiconname written nil on the line. Do not fill in the list of attributes directly (that is, delete the appicon60x60).
Create a new project for yourself
There are two issues to be aware of in your own implementation:
First, set the Info.plist.
As Apple's official API says, add cfbundleicons to the Info.plist first. But look at the key and Xcode name corresponding to the table, cfbundleicons corresponding to the none, then directly add Cfbundleicons Bar. Click the plus sign at the back of the Information property list, enter Cfbundleicons, and when you click the ENTER key, you'll be surprised to find that the cfbundleicons you added becomes the icon files (IOS 5) dictionary. Check out the official API for Cfbundleicons:
As you can tell, the key contains the icons information used by all apps. What's new is this:
The default includes Cfbundleprimaryicon and Uinewsstandicon. There is no cfbundlealternateicons, (I understand that Apple does not want to support the user to add an alternate icon, so it is not). Here is not too much to introduce uinewsstandicon, it should be shown on the Newstand, it is not clear, want to know can see the API, inside the introduction.
Now remove Newstand icon and add cfbundlealternateicons. After adding:
But look at the official API to Cfbundlealternateicon introduction inside, and there is no uinewstandbindingtype and Uinewsstandbindingedge, only this:
That is the official website said only Cfbundleiconfiles and Uiprerenderedicon, then delete the extra two uinewstandbindingtype and Uinewsstandbindingedge. Then add Uiprerenderedicon:
。 According to Apple, put the name of the alternate icon inside the Items0. We first add two images directly to the left navigation: [email protected] and [email protected], a 120 120, a 180 180.
But see say the settings, run up and run the code:
12345678910111213141516171819202122232425262728293031323334353637383940 |
@IBAction func changeToNewIconAction(_ sender: Any) {
if
!checkSupportChangeIcon() {
return
}
if
!UIApplication.shared.supportsAlternateIcons {
return
}
changeToIcon(
"newicon"
)
}
//MARK: check system version
func checkSupportChangeIcon() -> Bool {
let deviceVersion = UIDevice.current.systemVersion
if
deviceVersion.contains(
"10.3"
) {
return
true
}
return
false
}
//MARK: change to icon message
func changeToIcon(_ iconName: String?) {
UIApplication.shared.setAlternateIconName(iconName) { (error)
in
if
(error != nil) {
self.aler(str: (error!.localizedDescription))
}
else
{
self.aler(str:
"修改成功"
)
}
}
}
//MARK: alert message
func aler(str: String) {
let alert = UIAlertController.init(title:
"提示"
, message: str, preferredStyle: .alert)
let okAction = UIAlertAction.init(title:
"ok"
, style: .cancel) { (action: UIAlertAction)
in
print(
"关闭弹出框"
)
}
alert.addAction(okAction)
self.present(alert, animated:
true
, completion: nil)
}
|
The result is:
Look at the official API to Cfbundlealternateicons Introduction, there is a sentence:
1 |
in ios, the value of the key is a dictionary. the key for each Dictionary entry is the name of the alternate icon, which is also the string you pass to the setalternateiconname: completionhandler: method of uiapplication when changing icons. the value for each key is a dictionary containing the keys in table 5 |
This means that the key of the dictionary in this cfbundlealternateicons is the name of the alternate icon, so it needs to be modified:
That is, the key in the Cfbundlealternateicons dictionary is the name of the alternate icon, and then the dictionary with the name key contains the Cfbundleiconfiles and the Uiprerenderedicon.
This setting is then run again and you will find that the icon has been successfully changed.
This has succeeded in changing the icon.
About primary icon, do not set the icon files directly, if you want to set as the default icon, in the Setalternateiconname to pass in nil is good. This time info.plist source long this way:
In the property list, the icon already includes gloss effects of Primary icon is Uiprerenderedicon, which is set to false. (Here the gloss effect is not tested out what is not the same)
Now, let's see where the backup icon is.
Put it right here at the beginning:
The discovery is OK and can be displayed normally.
and put it in the assets.xcassets? Put it in the bundle? Next, test each case:
Put it inside the assets.xcassets. Create a new normal image set, then put the picture inside, the effect:
It has been tested that this placement does not change the appicon normally. But there was no error in running the results, and the new app icon was loaded in the system prompt:
Put it in the assets.xcassets, and the new icon,
The run is still set unsuccessfully. No error hints
Put it in a new bundle. :。
The result of the operation is still not successfully changed. There are no error prompts.
So after testing, it was found that only the images placed in the navigation can be changed successfully. In the Apple QA have seen a scene, they are directly in the navigation to create a new folder, and then put the picture inside, and then use. It is also recommended to set up a folder to put pictures and then use
Also, refer to the icon size here.
Finally, let's tell you a way to configure icon without using asset: Write directly under Info.plist:
This is generic and can be set directly on the iphone and ipad icon. In fact, here is also want to tell you: How to add a picture array inside the cfbundleiconfiles:
1 |
CFBundleIconFiles Icon-Small Icon-Small-40 Icon-Small-50 Icon Icon-60 Icon-72 |
Change the icon for iOS app (iOS10.3)