Overview
iOS 10.3 allows you to change your app icon programmatically. We can change our app icon programmatically.
Let’s get started.
Tool Used: Xcode 9.1, Swift 4+
Step 1: Setup Project
Create new Xcode project.
Write name of your project, we have use SwitchingAppIcon. (Make sure that you choose swift language) and Click on Next button.
Create two icons @2x and @3x for the iPhone, we named it as appicon_1@2x and appicon_1@3x. Add them in their appropriate places in the assets folder.
We can add alternative icons but not into the assets folder.
Step 2: Register Alternative icons
Register these alternative icons in our Info.plist. Add this to your Info.plist file.
For iPhone App
<key>CFBundleIcons</key> <dict> <key>CFBundleAlternateIcons</key> <dict> <key>appicon_2</key> <dict> <key>CFBundleIconFiles</key> <array> <string>appicon_2</string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> <key>appicon_3</key> <dict> <key>CFBundleIconFiles</key> <array> <string>appicon_3</string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> </dict> <key>CFBundlePrimaryIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string></string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> </dict>
Make sure alternative icons are named without @2x and @3x prefix.
For iPad App
<key>CFBundleIcons~ipad</key> <dict> <key>CFBundleAlternateIcons</key> <dict> <key>appicon_2</key> <dict> <key>CFBundleIconFiles</key> <array> <string>appicon_2</string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> <key>appicon_3</key> <dict> <key>CFBundleIconFiles</key> <array> <string>appicon_3</string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> </dict> <key>CFBundlePrimaryIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string></string> </array> <key>UIPrerenderedIcon</key> <false/> </dict> </dict>
Step 3: Write some code
Create 3 buttons in our storyboard which will change between our icons.
Connect them in your ViewController and add this code to change app icons
// MARK:- Actions extension ViewController{ @IBAction func btnTapAction(_ sender: UIButton){ // If false, alternate icons are not supported for the current process. if UIApplication.shared.supportsAlternateIcons { var alternateIconName: String? = nil switch sender.tag { case 2: // Change App icon 2 alternateIconName = "appicon_2" break case 3: // Change App icon 3 alternateIconName = "appicon_3" break default: break } // Pass `nil` to use the primary application icon. UIApplication.shared.setAlternateIconName(alternateIconName) } } }
Step 4: Test
Press on App icon 1 to set default App icon.
Press home button to see result.
Press on App icon 2 to set appicon_2 icon
Press home button to see result.
Press on App icon 3 to set appicon_2 icon
Press home button to see result.
Note:
Be careful to use a unique key name for each alternate icon
For more reference
– Apple Developer Documentation