Xcode-iOS10.3新特性如何动态修改APP图标

前言

iOS10.3开放了一个新的API,就是更换APP的Icon图标。说明:使用下面的方法Xcode必须升级到最新的8.3.1。

函数方法
- (void)setAlternateIconName:(NSString *)alternateIconName  completionHandler:(void (^)(NSError*error))completionHandler;

参数

alternateIconName 这个是替换图标的名称;
在Info.plist文件里面添加一个CFBundleAlternateIcons字段,如果你想显示应用的主图标,则设置字段的值为nil,键的主键是plist里面的CFBundleIcons字段。
completionHandler 当你执行修改图标的操作后,系统通过这个block来告知结果。
error 在成功时,此参数的值为零。如果出现错误,该参数包含指示所发生的事的alternateiconname属性的值保持不变的错误对象。
说明 使用此方法将应用程序的图标更改为其主图标或其替换图标之一。你可以仅在supportsalternateicons属性的值是可以改变图标。
你必须声明你的应用程序的主要和关键的cfbundleicons交替使用你的应用程序的Info.plist文件图标。
有关如何配置你的应用程序替代图标,看到关键信息属性列表的关键参考cfbundleicons描述。
更多参考官方文档:https://developer.apple.com/reference/uikit/uiapplication/2806818-setalternateiconname?language=objc

步骤:

image.png
image.png
image.png
image.png
image.png

需要的代码

<key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>ThirdIcon</key>
            <dict>
                <key>UIPrerenderedIcon</key>
                <true/>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>Third</string>
                </array>
            </dict>
            <key>SecondIcon</key>
            <dict>
                <key>UIPrerenderedIcon</key>
                <true/>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>Second</string>
                </array>
            </dict>
            <key>FristIcon</key>
            <dict>
                <key>UIPrerenderedIcon</key>
                <true/>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>Frist</string>
                </array>
            </dict>
        </dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
        </dict>
    </dict>
- (IBAction)changeFirst:(id)sender {
    
    [self changeIconWithString:@"FristIcon"];
}


- (IBAction)changeSecond:(id)sender {

    [self changeIconWithString:@"SecondIcon"];
}


- (IBAction)changeThird:(id)sender {

    [self changeIconWithString:@"ThirdIcon"];
}


- (void)changeIconWithString:(NSString *)string
{
    
    //当前设备的系统版本。这里的所有api都是10.3才能使用的
    if ([UIApplication sharedApplication].supportsAlternateIcons) {
        NSLog(@"可以更换APPicon");
    }else{
        NSLog(@"不可以更换APPicon");
        return;
    }
    
    NSString *iconName = [[UIApplication sharedApplication] alternateIconName];
    
    [[UIApplication sharedApplication] setAlternateIconName:string completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"error == %@",error);
        }
        NSLog(@"iconName == %@",iconName);
    }];
}

相关demo:https://github.com/tianzhilan0/ChangeAppIcon

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言iOS10.3开放了一个新的API,就是更换APP的Icon图标。说明:使用下面的方法Xcode必须升级到最新...
    俊俊吖阅读 1,295评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,359评论 25 708
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,166评论 6 13
  • 时钟在不停的转动,不管什么,都不会阻止它的脚步,就像你,离开我的世界,未曾归来! 走在雅典的街头,呼吸中都...
    不敬阅读 253评论 1 1