In this blog we will learn about How To Change App Icon Runtime in Swift.
Introduction
In iOS, developers can provide alternate app icons that users can switch between without needing to reinstall the app. iOS 10.3 finally gave developers the ability to change their app’s icon programmatically, although it takes a little work to set up.
Implementation
Follow the steps to change the App Icon.
Step 1:
First of all you need to create a new project where you will implement How To Change App Icon Runtime in Swift.
Step 2:
After that you need to create a UIViewController (ViewController).
1 2 3 4 5 6 7 |
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() label.text = "Change Icon" // Do any additional setup after loading the view. } } |
A ViewController is a fundamental building block of an app’s user interface. It manages a single screen of content and handles user interactions.
Step 3:
Create @2x & @3x image for App Icon.
Step 4:
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 5:
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) } } } |
Above mention code is useful to change icon at runtime.
UIApplication.shared.setAlternateIconName(name):
This method in Swift used to change the app’s icon at runtime.
Step 6:
In this step Now you need to call 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.
Conclusion
In this article we have discussed about How To Change App Icon Runtime in Swift.
I hope this blog is helpful to understand this topic.
You can check here how to set app icon in iOS.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.