Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

ios - Can I change app icon programmatically

I have two app icons built-in (free and premium), is it possible to replace free icon to premium icon programmatically after in-app purchase is completed successfully?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a new solution for this situation.

You can use

setAlternateIconName(_:completionHandler:)

iOS 10.3 is out with xcode 8.3.

10.3+

Update:

Usage is pretty simple, the key is to find out plist usage and note that you can not load assets from xcassets ( at least didnt work for me ). You need to add your icon files to project and prefer 180x180 images for quality.

You need to use "Icon files (iOS 5)", new Icon files does not work.

One last thing, when you change icon on a button click it will popup a Alert window that says 'You have changed the icon for "IcoTest".'

enter image description here

//ViewController.swift
@IBAction func ico1Click(_ sender: Any) {
    if UIApplication.shared.supportsAlternateIcons{
        UIApplication.shared.setAlternateIconName("Icon2", completionHandler: { (error) in
            print(error ?? "")
        })
    }else{
        print("NO NO")
    }
}

//Info.plist
<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>Icon1</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>alternater1_180x180</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>Icon2</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>alternater2_180x180</string>
            </array>
        </dict>
    </dict>
</dict>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...