修改 iOS AppIcon

有一次看到亚马逊的客户端打开后,就提示 icon 改变了,回桌面一看,竟然真的变了。然后就上网搜索是怎么实现的,参照着写了一个小 demo ,权当笔记。

修改应用图标

首先,导入图片到项目的中,如下图。不要导入到 Assets.xcassets ,否则无法修改。

项目结构

这之后,修改 Info.plist 如图:

Info.plist

对应部分的 XML 代码如下:

<key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>gray</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>github-gray</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <true/>
            </dict>
            <key>green</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>github-green</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <true/>
            </dict>
            <key>pink</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>github-pink</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <true/>
            </dict>
        </dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
    </dict>

最后的工作就剩调用 setAlternateIconName(_:completionHandler:)

func setAppIcon(to name: String?) {
        let app = UIApplication.shared
        guard app.supportsAlternateIcons else {
            return
        }
        app.setAlternateIconName(name) {
            if let error = $0 {
                print("Failed: \(error.localizedDescription)")
            } else {
                print("Aha, icon changed!")
            }
        }
    }

是不是小菜一碟呀?

附:

转载请注明出处: http://www.cnblogs.com/0xa6a

猜你喜欢

转载自www.cnblogs.com/0xa6a/p/8875649.html
今日推荐