iOS 应用内更换App图标

2025-12-28 02:20:04

1、新建Xcode测试工程,将需要的图标文件导入,在这里导入三套图片,简单起见命名为1,2,3,分别为60的2x和3x图片,如下:

iOS 应用内更换App图标

2、在官方文档的解释是CFBundleAlternateIcons字典下需要增加CFBundleIconFiles与UIPrerenderedIcon两个键对应新图标,具体的文档截图与大致翻译如下:

iOS 应用内更换App图标

iOS 应用内更换App图标

iOS 应用内更换App图标

3、选择Info.plist,在其下新增Icon files (iOS 5)字典,根据文档加入CFBundleAlternateIcons字典,具体如下:

<dict>

<key>CFBundleAlternateIcons</key>

<dict>

<key>2</key>

<dict>

<key>UIPrerenderedIcon</key>

<false/>

<key>CFBundleIconFiles</key>

<array>

<string>2</string>

</array>

</dict>

<key>3</key>

<dict>

<key>UIPrerenderedIcon</key>

<false/>

<key>CFBundleIconFiles</key>

<array>

<string>3</string>

</array>

</dict>

</dict>

<key>CFBundlePrimaryIcon</key>

<dict>

<key>CFBundleIconFiles</key>

<array>

<string>1</string>

</array>

<key>UIPrerenderedIcon</key>

<false/>

</dict>

</dict>

iOS 应用内更换App图标

4、Icon files (iOS 5)下的Primary Icon表示初始图片设置,在这里设置为图片1,如下:

iOS 应用内更换App图标

5、图标的更改是放在按钮事件中响应的,点击界面上的按钮完成图标的更改,具体实现代码如下:

if #available(iOS 10.3, *) {

            

            if UIApplication.shared.supportsAlternateIcons {

                

                UIApplication.shared.setAlternateIconName("2", completionHandler: {(error) in

                    

                    if error != nil {

                        

                        print(error)

                    }

                })

            }

        }

iOS 应用内更换App图标

6、编译运行下项目,然后回到模拟器的首页,可以看到模拟器当前项目中显示的图标为图片1,如下:

iOS 应用内更换App图标

7、回到App中,点击放置的按钮进行图标更换,更换成功会弹出提示框,点击ok,然后回到模拟器首页可以看到App图标已经更换掉,如下:

iOS 应用内更换App图标

iOS 应用内更换App图标

iOS 应用内更换App图标

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢