Change App icon programmatically

戏子无情 提交于 2019-12-10 19:36:37

问题


I have seen this application on iTunes, it is creating custom icon in iphone. In my application I also want to change icon , specifically what I want to do is in my icon there is one label and programmatically I want to change the value of label.


回答1:


From the video tutorial of the app, it seems like all they're doing is they created a web page with favicon of the custom icon that you created, then the user would tap "Add To Home Screen" to add the custom web page to the home screen. That should be enough to get you going.




回答2:


It is possible to change appIcon from iOS 10.3.

Swift 3:

if UIApplication.shared.supportsAlternateIcons{
        UIApplication.shared.setAlternateIconName("icon2", completionHandler: { (error) in
            print(error ?? "")
        })
}

Objective C:

[[UIApplication sharedApplication] setAlternateIconName:@"icon2" completionHandler:^(NSError * _Nullable error) {
        //NSLog(@"Error...");
}];

set supportsAlternateIcon to Yes in info.plist. Both primary and secondary icons should be added in CFBundleIcons key of your app's Info.plist file.

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

References:

  • Apple Document: setAlternateIconName(_:completionHandler:)
  • How to change your app icon dynamically with setAlternateIconName()



回答3:


This is not possible. Unless your app is of Newsstand category. For newsstand app, change the icon using the code,

UIApplication *app = [UIApplication sharedApplication];
[app setNewsstandIconImage:newsstandImage];

Note: What @Enrico suggests is a different solution. Your app icon will still be there in home screen, a duplicate icon will be created. Which most of the users wont prefer.




回答4:


Only my two cents.

Adding to plist directly is fine, the net effect is to have a "strange" value (IOS5...) in plist if seen visually in Xcode:

2) on simulator (Xcode 10 beta...) debug console on run you will see:

MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.

but works

3) don't call directly in AppDelegate. if needed so, call it dispatched:

    final func changeIcon(){

        let name = "Icon1"
        let icon = UIImage(named:  name)
        if UIApplication.shared.supportsAlternateIcons{
            UIApplication.shared.setAlternateIconName(name, completionHandler: { (error) in

                print(error ?? "ok")
            })
        }
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when) {
            self.changeIcon()
        }

        return true
    }
.....

4) note: icon name is the symbolic name you put in key in upper level, so for example:

<key>CFBundleIcons</key>
<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>Icon1</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>logo2_120x120</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>Icon2</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>logo3_120x120</string>
            </array>
        </dict>
    </dict>
</dict>

and do NOT add @2x or similar in plist.



来源:https://stackoverflow.com/questions/21669858/change-app-icon-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!