Two Methods for Avoiding Alert Display When Changing Icons in iOS

Diana Nareiko
2 min readFeb 19, 2024

--

When we switch the app icon, iOS displays an alert to the user informing that the icon has changed. Its look like that:

If you want to avoid the appearance of this alert for some reason, you can use the following two tricks:

The first one is not as safe as we would like, and you can see the code below:

func setApplicationIconName(_ iconName: String?) {
if UIApplication.shared.responds(to: #selector(getter: UIApplication.supportsAlternateIcons)) && UIApplication.shared.supportsAlternateIcons {

typealias setAlternateIconName = @convention(c) (NSObject, Selector, NSString?, @escaping (NSError) -> ()) -> ()

let selectorString = "_setAlternateIconName:completionHandler:"

let selector = NSSelectorFromString(selectorString)
let imp = UIApplication.shared.method(for: selector)
let method = unsafeBitCast(imp, to: setAlternateIconName.self)
method(UIApplication.shared, selector, iconName as NSString?, { _ in })
}
}

Apple doesn’t allow using these methods because they can change the name of the method, and this can lead to a crash. In addition, I want to mention that this method has been working since 2018, and... However, use it at your own risk ☺️.

The second one takes more time than the previous one, BUT it’s safer.

    func setApplicationIconName(_ iconName: String?) {
if UIApplication.shared.supportsAlternateIcons {
let blankViewController = UIViewController()
blankViewController.modalPresentationStyle = .custom
blankViewController.transitioningDelegate = blankViewControllerTransitioningDelegate
present(blankViewController, animated: false, completion: { [weak self] in
UIApplication.shared.setAlternateIconName(iconName)
self?.dismiss(animated: false, completion: nil)
})
}
}

This code snippet is used to change the app icon without triggering the alert message that typically appears when changing the app icon dynamically.

Here’s how it works:

1. Checking Compatibility:
UIApplication.shared.supportsAlternateIcons checks if the current device and iOS version support dynamic app icon changes. This ensures that the following steps will only be executed if the feature is supported.

2. Presentation of Blank View Controller:
— A UIViewController named blankViewController is created. It’s essentially a blank screen that will be presented to the user temporarily.
modalPresentationStyle is set to .custom to allow for a custom presentation style.
transitioningDelegate is set to blankViewControllerTransitioningDelegate, indicating that custom transition animations will be used to avoid flashing screen animation.
present(_:animated:completion:) is called to present `blankViewController` without any animation.

3. Changing the App Icon:
— Inside the completion block of the presentation, UIApplication.shared.setAlternateIconName(iconName) is called to set the desired alternate icon. iconName is assumed to be a variable containing the name of the alternate icon.
— This step is crucial as it changes the app icon programmatically.

4. Dismissal of Blank View Controller:
— After the app icon has been changed, dismiss(animated:completion:) is called to dismiss the blankViewController.

The full code of this method you can find here❤️

--

--