App icon doesn't show in UI when building with Xcode 16

I used to be able to show my app's icon in the UI using this code:

if let icon = UIImage(named: "AppIcon") {
    Image(uiImage: icon)
        .resizable()
        .frame(width: 64, height: 64)
        .cornerRadius(10)
}

But this doesn't work when building with Xcode 16 and iOS 18 SDK.

How can I show my app's icon in the UI without resorting to duplicating the asset?

Answered by DTS Engineer in 791165022

@rmahmud28 Could you verify the icon file name in the CFBundleIcons dictionary?

For example:

struct ContentView: View {
    @State private var iconFileName: String?
    var body: some View {
        VStack {
            if let iconFileName = iconFileName, let icon = UIImage(named: iconFileName) {
                Image(uiImage: icon)
                    .resizable()
                    .frame(width: 64, height: 64)
                    .cornerRadius(10)
            }
        }
        .task {
            guard
                let icons = Bundle.main.object(forInfoDictionaryKey: "CFBundleIcons") as? [String: Any],
                let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
                let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
                let iconFileName = iconFiles.last
            else {
                print("Could not find icons in bundle")
                return
            }
            self.iconFileName = iconFileName
        }
    }
}
Accepted Answer

@rmahmud28 Could you verify the icon file name in the CFBundleIcons dictionary?

For example:

struct ContentView: View {
    @State private var iconFileName: String?
    var body: some View {
        VStack {
            if let iconFileName = iconFileName, let icon = UIImage(named: iconFileName) {
                Image(uiImage: icon)
                    .resizable()
                    .frame(width: 64, height: 64)
                    .cornerRadius(10)
            }
        }
        .task {
            guard
                let icons = Bundle.main.object(forInfoDictionaryKey: "CFBundleIcons") as? [String: Any],
                let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
                let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
                let iconFileName = iconFiles.last
            else {
                print("Could not find icons in bundle")
                return
            }
            self.iconFileName = iconFileName
        }
    }
}

It seems showing AppIcon's within UIImage(named: String) is deprecated.

See the release notes for Xcode 16 beta 5:

If UIImage(named:) was called with the name of an app icon, it would previously return an undefined image of the icon set, without any masking or shadowing applied. On iOS 18, it now returns nil. To display an icon within the app itself, add the desired image to the asset catalog as an image set. (130491812) (FB14052579)

App icon doesn't show in UI when building with Xcode 16
 
 
Q