'How do I include iOS App Icon image within the app itself?
I have standard iOS app, with a standard app icon contained in Assets.
I'd like to display the app icon within the app (using SwiftUI). Note that I am not asking how to set the app icon, or change the icon dynamically. I just want to show the app icon within the app's own Settings view.
It would appear the App Icon asset should just be like any other, and I could include it using the following (note there is no space between App and Icon in the default icon naming),
Image("AppIcon")
I've also tried experimenting with,
Image("[email protected]") // Pick out a specific icon by filename
Image("icon_60pt@3x") // Maybe it assumes it's a .png
Image("icon_60pt") // Maybe it auto picks most appropriate resolution, like UIKit
...but none of these work.
How do I include the apps own icon within the app, without duplicating it as a separate Image Set (which I have tried, and does work.)
Thanks.
Solution 1:[1]
The following works if app icon is correctly set for used device (ie. iPhone icons for iPhone, etc.)
Note: sizes of app icons must match exactly!
Tested with Xcode 11.4
Image(uiImage: UIImage(named: "AppIcon") ?? UIImage())
Solution 2:[2]
This works:
extension Bundle {
var iconFileName: String? {
guard let icons = infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
let iconFileName = iconFiles.last
else { return nil }
return iconFileName
}
}
struct AppIcon: View {
var body: some View {
Bundle.main.iconFileName
.flatMap { UIImage(named: $0) }
.map { Image(uiImage: $0) }
}
}
You can then use this in any view as just:
AppIcon()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | barefeettom |