'iOS - Dynamic app icons for iPhone and iPad devices

I am trying to change the app icon dynamically using setAlternateIconName and I faced an issue in the iPhone X device where a blank icon is shown instead of the expected icon. This works fine on iPhone 6s, iPhone 8 and iPhone 13 pro devices (I use an M1 Mac and the project won't work on Simulators). Also I would like the dynamic app icon for the iPad target as well. The requirement is to support an additional app icon to the existing one.

enter image description here

The above icon images are added to the project bundle for the new icon.

The code for my app icon manager is as follows:

enum ONAppIcon: CaseIterable {
case normal,
hematology

var name: String? {
    switch self {
    case .normal:
        return nil
    case .hematology:
        return "icon-hem"
    }
}

    var preview: UIImage {
        switch self {
        case .normal:
            return #imageLiteral(resourceName: "[email protected]")
        case .hematology:
            if UIDevice.current.userInterfaceIdiom == .pad {
                return #imageLiteral(resourceName: "[email protected]")
            }
            return #imageLiteral(resourceName: "[email protected]")
        }
    }
}

class AppIconManager {
    static let shared = AppIconManager()
    var current: ONAppIcon {
        if #available(iOS 10.3, *) {
            return ONAppIcon.allCases.first(where: {
              $0.name == UIApplication.shared.alternateIconName
            }) ?? .normal
        }
        return .normal
    }

    func setIcon(_ appIcon: ONAppIcon, completion: ((Bool) -> Void)? = nil) {
  
        if #available(iOS 10.3, *) {
            guard current != appIcon,
              UIApplication.shared.supportsAlternateIcons
            else { return }
            UIApplication.shared.setAlternateIconName(appIcon.name) { error in
              if let error = error {
                print("Error setting alternate icon \(appIcon.name ?? ""): \(error.localizedDescription)")
              }
              completion?(error != nil)
            }
        }
    }
}

The Info.plist contents are as follows: enter image description here

  1. What are the app icon sizes to be added to support dynamic app icons for both iPhone and iPad devices?
  2. Is there any naming convention for the images?
  3. How to add the icon files correctly in the Info.plist file?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source