美文网首页
iOS 自动更换 App Icon

iOS 自动更换 App Icon

作者: Gavin_盖文 | 来源:发表于2023-01-28 19:53 被阅读0次

不重新上架更新 App Icon 图标

看到节假日手机里的App Icon没有更新情况下自动换了
版本要求:iOS 10.3

一、配置

Info.plist内添加Icon files (iOS 5),默认带两个key,这个不用管。按照下面格式添加新的key

    <key>CFBundleIcons</key>
    <dict>
<!--    这一部分是用户需要加进去的    -->
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>changeIcon1</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>tianxingjue</string>
                </array>
            </dict>
        </dict>
<!-- changeIcon1可自定义,调用时候用changeIcon1,可以有多个changeIcon2、changeIcon3等
                    tianxingjue是备用icon图片名称,图片放在工程里--------- -->
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconName</key>
            <string></string>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>UINewsstandIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UINewsstandBindingType</key>
            <string>UINewsstandBindingTypeMagazine</string>
            <key>UINewsstandBindingEdge</key>
            <string>UINewsstandBindingEdgeLeft</string>
        </dict>
    </dict>

二、代码部分

Swift

        if UIApplication.shared.supportsAlternateIcons {
            UIApplication.shared.setAlternateIconName(iconName) { error in
                if error != nil {
                    print("\(#fileID) line:\(#line) \(#function) 换icon发生错误,error: \(error!)")
                } else {
                    print("\(#fileID) line:\(#line) \(#function) 换icon完成.")
                }
            }
        }

OC

    if ([[UIApplication sharedApplication] supportsAlternateIcons]) {
        [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"%s line:%d 更新App图标失败,错误:%@", __func__, __LINE__, error);
            } else {
                NSLog(@"%s line:%d 更新App图标成功。", __func__, __LINE__);
            }
        }];
    }

上面两段代码都会弹出更换icon的提示框,体验很不好。


更换icon提示框

下一步是把这个提示框想办法干掉

尝试Swift方式取消弹窗无果。
最后用oc代码替换icon得以解决提示框问题:

+ (void)exchangeAlternateIcon:(NSString * _Nullable)iconName {
    NSString *selectorStr = @"_set";
    selectorStr = [selectorStr stringByAppendingString:@"Alternate"];
    selectorStr = [selectorStr stringByAppendingString:@"IconName:"];
    selectorStr = [selectorStr stringByAppendingString:@"completionHandler:"];
    
    SEL selector = NSSelectorFromString(selectorStr);
    
    IMP imp = [[UIApplication sharedApplication] methodForSelector:selector];
    void (*setIcon)(id, SEL, NSString *, id) = (void *)imp;
    if (setIcon) {
        setIcon([UIApplication sharedApplication], selector, iconName, ^(NSError *error) {
            if (error != nil) {
                NSLog(@"%s line:%d 更改App图标发生错误:%@", __func__, __LINE__, error);
            } else {
                NSLog(@"%s line:%d 更改App图标成功。", __func__, __LINE__);
            }
        });
    }
}

Swift内调用的话需要桥接一下。

Note:

unsafeBitCast:转换时会忽略数据类型的强制转换,不会因为数据类型的变化而改变原来的内存数据

参考:

iOS动态切换AppIcon
Swift 通过IMP调用Selector
swift的@convention

相关文章

网友评论

      本文标题:iOS 自动更换 App Icon

      本文链接:https://www.haomeiwen.com/subject/jcfuhdtx.html