Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include iOS App Icon image within the app itself?

Tags:

ios

swift

swiftui

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.

like image 229
Snips Avatar asked May 28 '20 11:05

Snips


People also ask

How can I add a picture to my app icon?

Tap Photo icon, then tap Add new. Set the size for the icon, then tap OK. Select the image you want to use for the app. Crop the image (choose Crop photo or Crop picture, then Always or Just once), then tap OK.

Can I use Apple icons in my app?

Apple products are copyrighted and can't be reproduced in your app icons.

Can you save a picture as an icon on Iphone?

Tap Add to Home Screen and select the icon next to your shortcut under Home Screen Name and Icon. In the pop-up window that appears, select File, Photo, or Take Photo. You can crop an image to customize what will show, but happily, the image needn't be square or a specific size. Tap Add > Done.


Video Answer


2 Answers

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

demo1

demo2

Image(uiImage: UIImage(named: "AppIcon") ?? UIImage())
like image 53
Asperi Avatar answered Oct 19 '22 03:10

Asperi


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()
like image 4
barefeettom Avatar answered Oct 19 '22 03:10

barefeettom