fbpx
How to change app icon programmatically in iOS using Swift

How to change app icon programmatically in iOS using Swift

In this tutorial, I’m going to show you how easy it is to change the icon of your iOS app programmatically.

Apple started to support this feature from the 10.3 version.

So, keep in mind that this method will work only on devices with iOS 10.3 and above.

Adding the alternate icon

First, create two sizes of your alternate icon, like in this example we have:

  • white-logo@2x.png = 120 x 120 pixels
  • white-logo@3x.png = 180 x 180 pixels

You have to add @2x and @3x after your icon name for the system to know which one to use in different screens

We’re going to add these icons inside our project folder, NOT in the Assets.xcassets, as we do with other icons/images

Open Xcode, go to your project folder and right-click > New Group and give a name to the new folder. (In this example, we call it WhiteLogo)

Drag-and-Drop your alternate icons inside the folder

Setting up the alternate icon

Now, we have to add the alternate icon in our Info.plist file.

Right-click on the Info.plist > Open As > Source Code

Paste the following code after the first <dict> tag and replace the WhiteLogo, with the name of the folder we created before, and the white-logo, with the name of your alternate icon.

If your icons are my-icon@2x.png and my-icon@3x.png, then the name is my-icon

<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>WhiteLogo</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>white-logo</string>
            </array>
        </dict>
    </dict>
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconFiles</key>
        <array/>
        <key>UIPrerenderedIcon</key>
        <false/>
    </dict>
</dict>

After you added it, the Info.plist should look like this:

Return your Info.plist back to Property List by going Right-click on the Info.plist > Open As > Property List

Changing the icon

To change the icon, we use the instance method setAlternateIconName(_:completionHandler:), but before that, we need to check if the system allows us to change the icon.

// Check if the system allows you to change the icon
if UIApplication.shared.supportsAlternateIcons {
    // ... Change the app icon inside here
}

And then change to alternate icon:

// Show alternative app icon
UIApplication.shared.setAlternateIconName("WhiteLogo") { error in
    if let error = error {
        print(error.localizedDescription)
    } else {
        print("App Icon changed!")
    }
}

If you want to return to the previous icon (default icon), just set nil as a parameter

// Show default app icon
UIApplication.shared.setAlternateIconName(nil)
You can find the final project here

If you have any questionsplease feel free to leave a comment below

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments