设置备用应用程序图标在iOS 10.3中返回错误3072:"操作已取消"

wgr*_*r03 7 icons ios swift swift3

我正在尝试为我的应用程序设置备用图标iOS 10.3,但每次调用方法Xcode都会返回:

错误域= NSCocoaErrorDomain代码= 3072"操作已取消."

我在这篇文章中使用@KlimczakM的答案来设置图标(下面称为方法),但我使用自己的方法从设置中加载首选图标:specifyIcon

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)
}
Run Code Online (Sandbox Code Playgroud)

在我的Info.plist我有五个图标; white,dark,rainbow,text,和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>
Run Code Online (Sandbox Code Playgroud)

所有这些图标都包含在我的应用程序包中名为"resources"的文件夹中的PNG文件中.

我该如何解决这个问题?

fly*_*ear 11

由于两个原因我得到了这个错误,

  • 首先,我没有通过将png文件添加到项目中来"将文件添加到'ProjectNameFoo'".否则它不起作用.之后它开始看到图标.
  • 其次,我收到此错误是因为我在viewDidLoad之后尝试更改图标.我不是为什么,但它给了我同样的错误.当我像下面的代码一样延迟尝试时,无论我给的是什么,它都在工作.

    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)
    }
    
    Run Code Online (Sandbox Code Playgroud)


use*_*197 9

我被这个错误困扰了很长一段时间并尝试了各种方法,但无法弄清楚我做错了什么。我正在从 更改图标AppDelegate.application(didFinishLaunchingWithOptions:)。按照上面的建议使用计时器延迟呼叫确实解决了它。

值得注意的是,这个问题是由于 UIKit 试图显示一个UIAlertController带有消息的

您已更改 $(PRODUCT_NAME) 的图标

这在当时似乎不起作用。您需要等到加载根视图控制器。

这是因为这个 API 并不是为了让开发者随意更新图标,而是为了用户的可交付性选择一个。