Reputation: 452
I am trying to change the app icon through code, but it does not seem to work. Below is my 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>
Here is the code I am using to change the icon:
@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.")
}
}
}
And finally, here is the folder with the app icons: App icon folder screenshot
As you can see, I am using the same name everywhere "icon.dark_60pt", but I still get an error "The file doesn’t exist."
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:]}
What am I doing wrong?
Upvotes: 1
Views: 1438
Reputation: 3359
I think you have to use the name of the icon, not the name of the file. Change the let name line with this:
let name = "AppIcon-2"
Upvotes: 2