我正在尝试通过代码更改应用程序图标,但它似乎不起作用。下面是我的 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.")
}
}
}
最后,这是带有应用程序图标的文件夹:应用程序图标文件夹截图
如您所见,我在所有地方都使用相同的名称“icon.dark_60pt”,但我仍然收到错误“文件不存在”。
由于错误域 = NSCocoaErrorDomain 代码 = 4“文件不存在”,应用程序图标失败。UserInfo={_LSLine=191,NSUnderlyingError=0x6000026e85d0 {错误域=LSApplicationWorkspaceErrorDomain Code=-105“在 CFBundleAlternateIcons 条目中找不到图标名”UserInfo={_LSLine=179,NSLocalizedDescription=在 CFBundleAlternateIcons 条目中找不到图标名,_LSFunction=-[LSAltIconManager _setAlternateIconName: forIdentifier:withIconsDictionary:error:]}}, _LSFunction=-[LSAltIconManager _setAlternateIconName:forIdentifier:withIconsDictionary:error:]}
我究竟做错了什么?