Mc.*_*ver 11 iphone xcode ipad ios swift
我在iPad上更改应用程序图标时遇到问题.一切都在iPhone上工作正常,但在iPad上我得到这个错误:
[default]无法将preferredIconName设置为AI-Gorgosaurus for ...:0> error:Error Domain = NSCocoaErrorDomain Code = 4"该文件不存在." UserInfo = {NSUnderlyingError = 0x600000248a30 {错误域= LSApplicationWorkspaceErrorDomain Code = -105"iconName在CFBundleAlternateIcons条目中找不到"UserInfo = {NSLocalizedDescription = iconName在CFBundleAlternateIcons条目中找不到}}}应用程序图标因文件不存在而失败.
我搜索广告发现,我需要补充~ipad的CFBundleIconFiles,但仍然得到同样的错误!
这是代码:
func changeIcon(to name: String?) {
//Check if the app supports alternating icons
guard UIApplication.shared.supportsAlternateIcons else {
return;
}
//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.")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚测试了另一个项目,工作正常!但不是我目前的项目为什么?!你知道吗?
Con*_*lon 14
试图提供此答案的更可重用(易于复制/粘贴)的版本。
1) 您需要添加以下名称和大小的图像作为常规 .png 文件(无资产目录)
.../AppIcon-Dark/iPad-app.png
pixelWidth: 76
.../AppIcon-Dark/iPad-app@2x.png
pixelWidth: 152
.../AppIcon-Dark/iPad-pro@2x.png
pixelWidth: 167
.../AppIcon-Dark/iPhone-app@2x.png
pixelWidth: 120
.../AppIcon-Dark/iPhone-app@3x.png
pixelWidth: 180
Run Code Online (Sandbox Code Playgroud)
然后您可以在 info.plist 中添加/插入以下内容
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>AppIcon-Dark</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>iPhone-app</string>
</array>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
</dict>
</dict>
<key>CFBundleIcons~ipad</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>AppIcon-Dark</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>iPad-app</string>
<string>iPad-pro</string>
</array>
</dict>
</dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
然后我用以下设置我的图标
func updateIcon() {
if #available(iOS 13.0, *) {
let dark = window?.traitCollection.userInterfaceStyle == .dark
let currentIconName = UIApplication.shared.alternateIconName
let desiredIconName:String? = dark ? "AppIcon-Dark" : nil
if currentIconName != desiredIconName {
UIApplication.shared.setAlternateIconName(desiredIconName) {
(error) in
print("failed: \(String(describing: error))")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ood 10
您的info.plist结构不正确。
你有:
- CFBundleIcons
- CFBundleAlternateIcons
- Icon Name
- CFBundleIconFiles
- CFBundleIconFiles~ipad
Run Code Online (Sandbox Code Playgroud)
但是应该是:
- CFBundleIcons
- CFBundleAlternateIcons
- Icon Name
- CFBundleIconFiles
- CFBundleIcons~ipad
- CFBundleAlternateIcons
- Icon Name
- CFBundleIconFiles
Run Code Online (Sandbox Code Playgroud)
基本上,一旦您可以使用iPhone图标CFBundleIcons,将其复制为CFBundleIcons~ipad。iPad文件根本不应嵌套在下面CFBundleIcons。
您正在混淆CFBundleIcons~ipad和CFBundleIconFiles~ipad(这不是有效的密钥)。