美文网首页
app 更换图标

app 更换图标

作者: 张俊凯 | 来源:发表于2018-06-27 21:20 被阅读3次

思路

  • iOS 10.3 以后开放了更换图标的接口,调用方法切换
  • 在mainbundle中放置要切换的图标文件
  • 在info.plist文件中配置相关参数告知系统要切换的图标的详细信息

解决步骤

  • bundle添加图片


    image.png
  • 配置info.plist文件,增加参数

    
    
    <key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>晴</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>晴20x20</string>
                    <string>晴29x29</string>
                    <string>晴40x40</string>
                    <string>晴60x60</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>jh</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>jh20*20</string>
                    <string>jh29*29</string>
                    <string>jh40*40</string>
                    <string>jh60*60</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>super</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>super20*20</string>
                    <string>super29*29</string>
                    <string>super40*40</string>
                    <string>super60*60</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
        </dict>
    </dict>

最终效果


image.png

左边的红圈名称是代码里需要传入的图片名称,右侧的红圈是系统根据不同的场景选择对应的图标

  • 最后关键性代码
[[UIApplication sharedApplication] setAlternateIconName:@"newicon" completionHandler:^(NSError * _Nullable error) {
            if (error) {
                //do something
            }
            
        }];
  • 补充:判断系统是否可以更换图标 返回是一个布尔值
[UIApplication sharedApplication].supportsAlternateIcons

判断app是否更换过图标

[[UIApplication sharedApplication] alternateIconName]

返回为null表示系统用的是默认icon,返回那么则表示正在使用更换后的图标

完。

=========================06-28补充==========
更换图标成功后,系统会默认弹出对话框,如果想不显示对话框,思路是在执行方法的控制器里拦截弹出对话框,方法是用runtime交换到自己的方法,在自己的方法里盘进行过滤,如果是切换图标的提示,不做任何操作,剩余情况全部按照原方法执行。过滤的条件是切换图标提示的alert,标题和提示内容均为空,以下是实现代码

//在viewdidload方法里执行此方法,进行交换
- (void)exchangeMethod{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalMethod = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
        Method newMethod = class_getInstanceMethod(self.class, @selector(jh_presentViewController:animated:completion:));
        // 交换方法实现
        method_exchangeImplementations(originalMethod, newMethod);
    });
}


// 最终的目的是 不显示alert弹窗
- (void)jh_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    
    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
        
        //换图标的提示为空
        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) { // 两者皆为空 不做任何处理
            return;
        } else {//已经经过交换,走系统正常的方法
            [self jh_presentViewController:viewControllerToPresent animated:flag completion:completion];
        }
        
    }else{//已经经过交换,走系统正常的方法
         [self jh_presentViewController:viewControllerToPresent animated:flag completion:completion];
    }

}

参考链接:http://www.cocoachina.com/ios/20170619/19557.html
完。

相关文章

  • Flutter 更换App图标

    以Flutter2.0+版本为示例 iOS App图标更换 Android App图标更换

  • iOS 无弹框更换 app 图标

    上篇文章我们详细查看了更换App图标的使用方法,并做了个小Demo。尽管当前我们可以实现动态更换App图标了,但是...

  • 【iOS 开发】iOS 无弹框更换 app 图标

    上篇文章我们详细查看了更换App图标的使用方法,并做了个小Demo。尽管当前我们可以实现动态更换App图标了,但是...

  • Android studio 更换默认应用图标后还是显示之前的i

    最近用vivo的手机测试app,没换app logo图标先运行了一遍,更换logo图标后再次运行发现还是之前的图标...

  • app 更换图标

    思路 iOS 10.3 以后开放了更换图标的接口,调用方法切换在mainbundle中放置要切换的图标文件在inf...

  • iOS更换APP图标

    今天,在使用'破壳日'的时候,无意中发现APP中有更换icon这一个功能.如下图所示 程序员的好奇心使我度娘了一番...

  • react native 图标

    ios更换图标 xcode打开项目 项目/General/App Icons and Launch Images/...

  • 【iOS】动态更换 App 图标

    动态更换图标 在iOS 10.3苹果添加了更换图标的功能,通过这个功能,我们可以在适当的时候采取特定的方式为我们的...

  • dailyLearning -- 动态更换 App 图标

    前言 动态更换App图标这件事,在用户里总是存在需求的:有些用户喜欢“美化”自己的手机。至于用户们喜欢美化到什么程...

  • iOS 动态更换App图标

    该功能应用的场景 1、白天/夜间模式切换,在切换App主色调同时切换App图标。 2、各类皮肤主题(淘宝就可换肤)...

网友评论

      本文标题:app 更换图标

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