iOS 10.3 finally gave developers the ability to change their app’s icon programmatically, although it takes a little work to set up. In Apple’s UIApplication
API document, there are 3 things worth a glance:
1 2 3 4 5 6 7 8 |
//A Boolean value indicating whether the app is allowed to change its icon. To make it true, we need to set up alternative icons in Info.plist file. var supportsAlternateIcons: Bool //The name of the icon being displayed for the app. Notice that it is nil when app is displaying its primary icon. var alternateIconName: String? //Changes the app's icon. If we set the icon name to nil, the app will display its primary icon. func setAlternateIconName(String?, completionHandler: ((Error?) -> Void)? = nil) |
Follow the steps to change the App Icon.
Step 1:
Create @2x & @3x image for App Icon.
Step 2:
The actual code to change your app’s icon is trivial, but first, there’s some setup work because you must declare all possible icons in your Info.plist file. The process behind this is far from optimal, and right now the best thing to do is edit your Info.plist as XML rather than trying to use the built-in property list editor in Xcode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<key>CFBundleIcons</key> <dict> <key>CFBundleAlternateIcons</key> <dict> <key>Icon1</key> <dict> <key>CFBundleIconFiles</key> <array> <string>Icon1</string> </array> </dict> <key>Icon2</key> <dict> <key>CFBundleIconFiles</key> <array> <string>Icon2</string> </array> </dict> </dict> </dict> |
Step 3:
Add a Method to change the image Icon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func changeIcon(name: String?) { //Check if the app supports alternating icons guard UIApplication.shared.supportsAlternateIcons else { return } guard let name = name else { UIApplication.shared.setAlternateIconName(nil) return } //Change the icon to a specific image with given name UIApplication.shared.setAlternateIconName(name){ error in if let error = error { print(error.localizedDescription) } } } |
Step 4:
Now call the changeIcon method from any event.
1 |
self.changeIcon(name: "Icon1") |
Hope the above blog will help you to change the App icon in runtime in iOS using swift.