Code=3072 "The operation was cancelled" when setting alternate app icon
Asked Answered
S

2

21

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?

Spotweld answered 13/2, 2017 at 1:23 Comment(0)
W
23

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 deliberately choose one.

Wicks answered 19/11, 2019 at 11:34 Comment(1)
This should be the right answer. The delay solution is just wrong.Ramie
L
31

I was getting this error because of two reasons,

  • First(and highly not because of), I didn't do "Add Files to 'ProjectNameFoo'" by adding png file to project. Otherwise it didn't work. After that it started to see icon.
  • Secondly(and highly possible reason), I was getting this error because I was trying to change the icon in 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")
    }
}
Lisalisabet answered 24/5, 2017 at 7:14 Comment(3)
@Lisalisabet did you find a reason for this issue ? I mean why does it not work without the delay ?Dowel
That was a free time project for me to present something. So. unfortunately, I didn't go on it on more after that. But if you find, please let me know too.Lisalisabet
For anyone who still couldn't fix it... I had to take a delay of 3.0 seconds in order to have it working.Arris
W
23

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 deliberately choose one.

Wicks answered 19/11, 2019 at 11:34 Comment(1)
This should be the right answer. The delay solution is just wrong.Ramie

© 2022 - 2024 — McMap. All rights reserved.