There is a method to change the app icon when the user click a button?
Thanks in advance.
i’m using:
Interface Builder: Storyboard
Language: Swift
macOS: 12
Xcode Version: 13.1
There is a method to change the app icon when the user click a button?
Thanks in advance.
i’m using:
Interface Builder: Storyboard
Language: Swift
macOS: 12
Xcode Version: 13.1
2
Answers
step1:
Design your alternative App Icons and export them in two sizes:
120px (60px @2x)
180px (60px @3x)
Add the icons to your project under a new directory App Icons. Note that the alternate icons files must to be in the Project directory, not in the Assets directory.
Step 2: Register your new Icons in the Info.plist file
First, add a new CFBundleIcons entry (Icon files (iOS 5)), then add another entry CFBundleAlternateIcons.
CFBundleAlternateIcons entry in Info.plist
For each alternate icon, add a new entry in the infos.plist file, under CFBundleAlternateIcons. The name of the entry is the name of the icon which will be used later in your Xcode project, and the string value of the item is the name of the icon file that you added in the project at Step 1.
App icons entries in Info.plist
Once you’ve added all your icons in Info.plist, your alternate icons are ready to be used in your App.
Step 3: The App Icon Manager
The Apple API to switch App Icons is quite simple and consists of 3
var/functions:
As per the Apple Documentation, supportsAlternateIcons will be true when the system allows you to change the icon of then App, or false otherwise.
The setAlternateIconName method is used to change the App Icon to its primary icon or to one of its alternate icons. If alternateIconName is nil then the default App Icon will be used.
Finally, alternateIconName returns the name of the alternate icon currently used, or nil if the default icon is used.
To handle icon changes easily, we will create an Icon Manager to interact with Apple APIs. First, create an enum containing each of your alternate App Icons.
Now let’s add the file name of each of our icon in the enum, and a preview icon that will be displayed in our App UI. In our enum, classic is the default app icon. That’s why the file name for it will be nil. For more information on why the file name is nil you can check the alternateIconName description on Apple documentation.
Now that we have our enum, let’s create an AppIconManger class with two functions: one to retrieve the current App Icon, and one to update it.
Step 4: Use your App Icon Manager in your App
Final step, to update the current App Icon, just call the setIcon function you previously defined and pass the new icon you want to set as parameter.
There is an easier way to change the application icon
And now you can change the app icon using the setAlternateIconName method. For example: