How to change your app icon dynamically with setAlternateIconName:completionHandler:




 

 

With iOS 10.3 developers have the ability to change their app’s icon programmatically,

The icon  should be placed loose in our project or in a group, rather than inside an asset catalogue. 

Also don’t forget to use the @2x and @3x naming convention to ensure iOS automatically picks the correct icon for users’ devices.

For my example, our icon files are



  • icon1.png, icon1@2x.png, icon1@3x.png
  • icon2.png, icon2@2x.png, icon2@3x.png
  • icon3.png, icon3@2x.png, icon3@3x.png

Note: icon images are of size

@1x are 76×76

@2x are 120×120

@3x are 180×180.

In  my project navigator, right-click at Info.plist file, and choose Open As > Source Code.

This will open the raw XML code of our plist

Now place the below code in your XML

<key>CFBundleIcons</key>

<dict>

<key>CFBundleAlternateIcons</key>

<dict>

<key>9to5_icon1</key>

<dict>

<key>CFBundleIconFiles</key>

<array>

<string>icon1</string>

</array>

<key>UIPrerenderedIcon</key>

<false/>

</dict>

<key>9to5_icon2</key>

<dict>

<key>CFBundleIconFiles</key>

<array>

<string>icon2</string>

</array>

<key>UIPrerenderedIcon</key>

<false/>

</dict>

<key>9to5_icon3</key>

<dict>

<key>CFBundleIconFiles</key>

<array>

<string>icon3</string>

</array>

<key>UIPrerenderedIcon</key>

<false/>

</dict>

</dict>

<key>CFBundlePrimaryIcon</key>

<dict>

<key>CFBundleIconFiles</key>

<array>

<string>9to5AppDefaultIcon</string>

</array>

</dict>

</dict>

 

here we have key name and icon file name, our keyname we use in our code (program) to set icons using plist.

key name 9to5_icon1 icon file name icon1

key name 9to5_icon2 icon file name icon1

key name 9to5_icon3 icon file name icon1

 



Inside my viewController.m, i have used below methods to change app_icons

// To change App icon 1

– (IBAction)btn_ChangeIcon1:(id)sender {

    if ([[UIApplication sharedApplication] supportsAlternateIcons] == NO)

        return;

    

    [[UIApplication sharedApplication] setAlternateIconName:@”9to5_icon1″

                                          completionHandler:^(NSError * _Nullable error) {

                                              NSLog(@”%@”, [error description]);

                                          }];

}

// To change App icon 2

– (IBAction)btn_ChangeIcon2:(id)sender {

    if ([[UIApplication sharedApplication] supportsAlternateIcons] == NO)

        return;

    

    [[UIApplication sharedApplication] setAlternateIconName:@”9to5_icon2″

                                          completionHandler:^(NSError * _Nullable error) {

                                              NSLog(@”%@”, [error description]);

                                          }];

}

// To change App icon 3

– (IBAction)btn_ChangeIcon3:(id)sender {

    if ([[UIApplication sharedApplication] supportsAlternateIcons] == NO)

        return;

    

    [[UIApplication sharedApplication] setAlternateIconName:@”9to5_icon3″

                                          completionHandler:^(NSError * _Nullable error) {

                                              NSLog(@”%@”, [error description]);

                                          }];

}

// To reset App icon  we need to setAlternateIconName as nil

– (IBAction)btn_ResetIcon:(id)sender {

    if ([[UIApplication sharedApplication] supportsAlternateIcons] == NO)

        return;

        [[UIApplication sharedApplication] setAlternateIconName:nil

                                          completionHandler:^(NSError * _Nullable error) {

                                              NSLog(@”%@”, [error description]);

                                          }];

}

 



From Apple Developer Document :

Apple Method : setAlternateIconName:completionHandler:

Used for: Changes the app’s icon.

Declaration:

– (void)setAlternateIconName:(NSString *)alternateIconName

           completionHandler:(void (^)(NSError *error))completionHandler;

Parameters:

alternateIconName

The name of the alternate icon, as declared in the CFBundleAlternateIcons key of your app’s Info.plist file. Specify nil if you want to display the app’s primary icon, which you declare using the CFBundlePrimaryIcon key. Both keys are subentries of the CFBundleIcons key in your app’s Info.plist file.

completionHandler

A handler to be executed with the results. After attempting to change your app’s icon, the system reports the results by calling your handler. (The handler is executed on a UIKit-provided queue, and not necessarily on your app’s main queue.) The handler has no return value and takes the following parameter:

error

On success, the value of this parameter is nil. If an error occurred, this parameter contains the error object indicating what happened and the value of the alternateIconName property remains unchanged.

Download Source Code Here



 

Add a Comment

Your email address will not be published. Required fields are marked *