Change app icon dynamically

Nidhi Suhagiya
3 min readMar 20, 2022

--

From iOS 10.3+ you are able to change the app icon dynamically or based on the selection from the provided icons in app. For this, apple provides a method called setAlternateIconName(_:completionHandler:).

But, you can change it only if the value of the supportsAlternateIcons property is true.

Declaration:

func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Void)? = nil)

Here,

alternateIconName: As the name suggest, you have to pass the name of the alternate icons which is used to display instead of the main app icon. Pass nil if you want to display primary icon of the app.

completionHandler: The code inside this closure will be executed after the completion of the method call. After attempting to change the app’s icon, the system reports the result by calling completionHandler. If it fails to change the icon, then it will return an error parameter. The value of the error parameter will be nil on success otherwise it contains an error object and the value of the alternateIconName property remains unchanged.

Let’s dive into the implementation:

You can achieve this in two ways.

  1. Specify the names of additional icon assets available to your app using the “Alternate App Icon Sets” in the build setting.

Xcode uses these setting to generate the entries for CFBundlePrimaryIcon and CFBundleAlternateIcons under the top-level key CFBundleIcons.

Steps:

  • Add additional AppIcons in assets
  • Go to the build settings of your target app
  • Search alternate app icon Sets and Add alternate icons names to “Alternate App Icon Sets”

2. Another way is you have to add CFBundlePrimaryIcon and CFBundleIcons keys in your Info.plist file.

You can add AppIcons either in your assets or in seperate folder under your workspace.

If you add Alternate AppIcons under assets, then set value of IncludeAllApp Icon Assets attribute to YES.

Source code to add inside info.plist is as below:-

Or property list of Info.plist will looks like this:

After adding appIcons and doing required configuration, inside your view controller, check if app supports alternate icons or not using supportsAlternateIcons as shown below:

if UIApplication.shared.supportsAlternateIcons {
// set a new app icon
}

Then Set alternate icons using setAlternateIconName() method as shown below:

UIApplication.shared.setAlternateIconName(“AppIcon-2”) { error in
print(“Error to set alternate icons:- \(error?.localizedDescription)”)
}

That’s it. You are ready to go.

You can check code here:- https://github.com/NidhiSuhagiya/DynamicAppIcon

--

--