in flutter, for ios app icon should change dynamically

60 Views Asked by At

for ios, I tried changing app icon and its changing to default swift app icon but not the one which I wanted to apply.

firstly i created app icons and dumped in project folder, then i created method channel and modified appDelegate file and info.plist file as mentioned below

info.plist:

<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>app_icon_b</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>app_icon_b</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>app_icon_g</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>app_icon_g</string>
            </array>
            <ke
            <false/>
        </dict>
    </dict>
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconName</key>
        <string></string>
        <key>CFBundleIconFiles</key>
        <array>
            <string>app_icon_g</string>
        </array>
        <key>UIPrerenderedIcon</key>
        <false/>
    </dict>
    <key>UINewsstandIcon</key>
    <dict>
        <key>CFBundleIconFiles</key>
        <array>
            <string></string>
        </array>
        <key>UINewsstandBindingType</key>
        <string>UINewsstandBindingTypeMagazine</string>
        <key>UINewsstandBindingEdge</key>
        <string>UINewsstandBindingEdgeLeft</string>
    </dict>
</dict>

appDelegate:

import UIKit import Flutter import Foundation

@UIApplicationMain @objc class AppDelegate: FlutterAppDelegate {

override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool {

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let mlkitChannel = FlutterMethodChannel(name: "com.dynamicIcon",
                                              binaryMessenger: controller.binaryMessenger)
mlkitChannel.setMethodCallHandler({
      (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
    if call.method == "launcherFirst"{
        if #available(iOS 10.3, *) {
            let iconName = "app_icon_g";
            self.setIcon(iconName)
        }else {
            result("Not supported on iOS ver < 10.3");
               }
        /* End */
        }else if call.method == "launcherSecond"{
            /* Start */
            if #available(iOS 10.3, *) {
                let iconName = "app_icon_b";
                self.setIcon(iconName)
            }else {
                result("Not supported on iOS ver < 10.3");
                   }
            /* End */
            }else if call.method == "default"{
                    /* Start */
                    if #available(iOS 10.3, *) {
                        let iconName = "app_icon_g";
                        self.setIcon(iconName)
                    }else {
                        result("Not supported on iOS ver < 10.3");
                           }
                    /* End */
                    }

} )

GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)

}

func setIcon(_ appIcon: String) {
    print(appIcon)
        if UIApplication.shared.supportsAlternateIcons{
            UIApplication.shared.setAlternateIconName(appIcon) { error in
                if let error = error {
                    print("Error setting alternate icon \(appIcon): \(error.localizedDescription)")
                }
            }
            print(UIApplication.shared.alternateIconName ?? "def")
        }
    }

}

print(UIApplication.shared.alternateIconName) i am getting the correct app_icon name but its not applying in the app instead default empty icon is applied(this one: https://www.google.com/url?sa=i&url=https%3A%2F%2Fdeveloper.apple.com%2Fdesign%2Fresources%2F&psig=AOvVaw0IxCKxnZzg3ffMl8TNZlfS&ust=1700556656445000&source=images&cd=vfe&opi=89978449&ved=0CBIQjRxqFwoTCJihv62Z0oIDFQAAAAAdAAAAABAE). please help

0

There are 0 best solutions below