Skip to content
DeveloperMemos

Changing an App's Icon Programmatically with Swift

Swift, iOS Development, App Icon2 min read

iOS developers know that app icons are essential elements of an app's visual identity. A well-designed app icon can capture the user's attention and increase the likelihood of downloads. Traditionally, iOS developers have had to ship a single app icon with their app, but in recent versions of iOS, Apple has added the ability to add alternative app icons. In this article, we will explore how to add alternative app icons to an app and switch between them programmatically using Swift.

Prerequisites

Before we dive into the code, there are a few prerequisites that you should be aware of. First, this feature is only available in iOS 10.3 and later, so make sure that you're running a compatible version of iOS. Second, alternative icons must be provided as iamge assets in the app bundle.

Adding Alternative Icons to an App

To add alternative icons to an app, you need to add the icon files to your app's asset catalog. Here are the steps:

  1. Open your app's Xcode project and select the asset catalog.
  2. Click the "+" button in the lower-left corner of the asset catalog window.
  3. Select "New iOS App Icon" from the pop-up menu.
  4. Enter a name for the new icon set and click "Create."
  5. Drag and drop the alternative icon image files into the new icon set(you should also be able to use a "Single Size" icon on Xcode 14+, which makes life a lot easier!).

Congratulations! You have added an alternative icon to your app. Now, let's see how to switch between them programmatically.

Changing an App's Icon Programmatically

To change an app's icon programmatically, you need to use the UIApplication.shared.setAlternateIconName(_:completionHandler:) method. Here's an example of how to use it:

1if UIApplication.shared.supportsAlternateIcons {
2 UIApplication.shared.setAlternateIconName("AppIconPink") { error in
3 if let error = error {
4 print("Error changing app icon: \(error.localizedDescription)")
5 } else {
6 print("App icon changed successfully.")
7 }
8 }
9}

In this example, we check whether the device supports alternative icons using the supportsAlternateIcons property. If it does, we call the setAlternateIconName(_:completionHandler:) method and pass the name of the alternative icon that we want to set. The method takes an optional completion handler that we can use to handle any errors that occur during the icon change.

Wrap Up

Changing an app's icon programmatically can add a lot of value to your app by allowing users to personalize their experience. In this article, we covered the necessary steps to add alternative icons to your app's asset catalog and how to switch between them at runtime using Swift. As always, it's important to test your app thoroughly before submitting it to the App Store to ensure that everything works as expected.