Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 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 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...