Setting Alternate App Icon returns error 3072: “The operation was cancelled” in iOS 10.3

南楼画角 提交于 2019-12-09 17:05:10

问题


I am trying to set the alternate icons for my app in iOS 10.3, but every time I call the method Xcode returns:

Error Domain=NSCocoaErrorDomain Code=3072 "The operation was cancelled."

I am using @KlimczakM's answer from this post to set the icon (the specifyIcon method called below) but I am using my own method to load the preferred icon from settings:

let iconSetting = userDefaults.string(forKey: "appIconSetting")
print("The icon setting is: \(iconSetting ?? "error getting appIconSetting.")")

switch iconSetting! {
case "white":
    specifyIcon(nil)
case "dark":
    specifyIcon("dark")
case "text":
    specifyIcon("text")
case "textdark":
    specifyIcon("textdark")
case "rainbow":
    specifyIcon("rainbow")
default:
    specifyIcon(nil)
    print("ERROR setting icon.")
}

func specifyIcon(_ icon: String?) {
    //(@KlimczakM's answer)
}

In my Info.plist I have five icons; white, dark, rainbow, text, and textdark:

<key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>white</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>ic_white</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>dark</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>ic_dark</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>rainbow</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>rainbow</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>text</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>ic_text</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
            <key>textdark</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>ic_textdark</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
        </dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>ic_white</string>
            </array>
        </dict>
    </dict>

All of these icons are contained as PNG files in a folder called "resources" inside my app bundle.

How can I fix this issue?


回答1:


I was getting this error because of two reasons,

  • First, 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, I was getting this error because I was trying to change icon after in viewDidLoad. I don't why but it was giving me same error. When I try with a delay like the code below it was working whatever second I gave.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        delay(0.01) {
            if foo().isFoo() {
                print("")
    
                self.changeIcon(name: "ColdRabbit")
            }
            else {
                print("")
            }
        }
    }
    
    func delay(_ delay:Double, closure:@escaping ()->()) {
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
    }
    



回答2:


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)

for debug builds (!?), and that didn't seem to work at that point.

It will not show this alert in release build configurations and change the icon just fine.



来源:https://stackoverflow.com/questions/42195325/setting-alternate-app-icon-returns-error-3072-the-operation-was-cancelled-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!