Hello iOS Devs,
Hope you guys doing well in this COVID-19 situation. Stay Safe At Home.In this week Xcode 11.4 released. Many new things came with that. You can check What’s new in Xcode 11.4? blog for the same.
App icon change functionality is available from iOS 10.3. In this tutorial we will learn to set an alternate icon for an iOS application programmatically.
Let’s Start!
Pre-Requirements
We require one default icon and another alternate icon. Alternate icon will be in @2x and @3x sizes. So the file name would be YourAlternateIcon@2x.png (120 x 120) and YourAlternateIcon@3x.png (180 x 180).
Add your app icon in Assets.xcassets as follows:
Add your alternate app icons in your project as follows (I have created Resources folder for it):
Ready your Info.plist for Alternate App Icon
Update your plist file for alternate app icon as follows:
- Create new row and select “Icon files (iOS 5)” in Info.plist.
- Create new entry CFBundleAlternateIcons
- Create new entry for alternative icon (i.e. JerryIcon – Dictionary)
- Register your alternate icon file name to item in CFBundleIconFiles
Here I am adding Info.plist as source code to see what you need to update in your plist:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<key>CFBundleIcons</key> <dict> <key>CFBundleAlternateIcons</key> <dict> <key>JerryIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string>JerryIcon</string> </array> </dict> <key>TomIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string>TomIcon</string> </array> </dict> </dict> <key>CFBundlePrimaryIcon</key> <dict> <key>CFBundleIconFiles</key> <array/> <key>UIPrerenderedIcon</key> <false/> </dict> <key>UINewsstandIcon</key> <dict> <key>CFBundleIconFiles</key> <array> <string></string> </array> <key>UINewsstandBindingEdge</key> <string>UINewsstandBindingEdgeLeft</string> <key>UINewsstandBindingType</key> <string>UINewsstandBindingTypeMagazine</string> </dict> </dict> |
Let’s Do The Code
List of methods provided by Apple which is used to Managing the App’s Icon:
We will use setAlternateIconName method to change icon for the application.
1 2 3 4 5 6 7 8 9 10 11 |
func changeIcon(iconName: String) { if UIApplication.shared.supportsAlternateIcons { UIApplication.shared.setAlternateIconName(iconName){ error in if let error = error { print(error.localizedDescription) } else { print("Done!") } } } } |
To use default/primary app icon use following code:
1 |
UIApplication.shared.setAlternateIconName(nil) |
Note: Whenever you set alternate icon, Apple will display default alert like: You have changed the icon for “YourApplicationName” You can check in following video.
Final Output!
This video is posted on my youtube channel. Like, Share and Subscribe for the content related to iOS Development, Swift Language and Apple News!
Sample code is available at my github.
I hope you learned something. If you did, feel free to share this article with a friend or on social media. Let me know if you have any questions, comments or feedback – either via Twitter or email.
Stay Safe At Home. Learn Something New. Share To The World.
Happy Coding 🙂