更改应用程序图标 iPhone 错误 "The file doesn’t exist."

Changing app icon on iPhone error "The file doesn’t exist."

我正在尝试通过代码更改应用程序图标,但它似乎不起作用。下面是我的 info.plist

<key>CFBundleIcons</key>
<dict>
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconFiles</key>
        <array>
            <string>icon_60pt</string>
        </array>
    </dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>AppIcon-2</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>icon.dark_60pt</string>
            </array>
        </dict>
    </dict>
</dict>

这是我用来更改图标的代码:

@objc func changeIcon() {
    //Check if the app supports alternating icons
    guard UIApplication.shared.supportsAlternateIcons else {
        return;
    }
    
    let name = "icon.dark_60pt"
    //Change the icon to a specific image with given name
    UIApplication.shared.setAlternateIconName(name) { (error) in
        //After app icon changed, print our error or success message
        if let error = error {
            print("App icon failed to due to \(error.localizedDescription)")
        } else {
            print("App icon changed successfully.")
        }
    }
}

最后,这是包含应用程序图标的文件夹:App icon folder screenshot

如您所见,我到处都使用相同的名称“图标。dark_60pt”,但我仍然收到错误“文件不存在”。

App icon failed to due to Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist." UserInfo={_LSLine=191, NSUnderlyingError=0x6000026e85d0 {Error Domain=LSApplicationWorkspaceErrorDomain Code=-105 "iconName not found in CFBundleAlternateIcons entry" UserInfo={_LSLine=179, NSLocalizedDescription=iconName not found in CFBundleAlternateIcons entry, _LSFunction=-[LSAltIconManager _setAlternateIconName:forIdentifier:withIconsDictionary:error:]}}, _LSFunction=-[LSAltIconManager _setAlternateIconName:forIdentifier:withIconsDictionary:error:]}

我做错了什么?

我认为你必须使用图标的名称,而不是文件的名称。用这个更改 let name 行:

let name = "AppIcon-2"