I am trying to set an alternate app icon named MyIcon
in my iOS app. I have a MyIcon.png
image in my project bundle (not in my Assets folder) and it is declared in my Info.plist like so:
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>MyIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>MyIcon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
</dict>
In my code, I try to change the app icon:
UIApplication.shared.setAlternateIconName("MyIcon")
However, the icon doesn't change and I am instead given this error in the console:
Error Domain=NSCocoaErrorDomain Code=3072 "The operation was cancelled."
How can I fix this issue?
I was getting this error because of two reasons,
viewDidLoad
. When I try with a delay like the code below it was working whatever second I gave.override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
UIApplication.shared.setAlternateIconName("MyIcon")
}
}
I was stuck with this error for quite a while and tried all sorts of things but couldn't work out what I was doing wrong. I was changing the icon from AppDelegate.application(didFinishLaunchingWithOptions:)
. Delaying the call with a timer as suggested above did solve it.
It is worth noting that this problem is due to UIKit trying to show a UIAlertController
with the message
You have changed the icon for $(PRODUCT_NAME)
and that didn't seem to work at that point. You need to wait until a root view controller is loaded.
This is because this API is not meant to for the developer to update the icon arbitrarily, but for the user to deliverability choose one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With