Dynamically Change the AppIcon

I found that the AppIcon of Xcode14 is more convenient. So I want to use SwiftUI to test it in the Xcode14.

  • Interface: SwiftUI
  • Language: Swift
  • Xcode: 14

We can found that when we click the button, the icon changes.

image

image

In the picture we can see that there is only 1024*1024px in AppIcon of Xcode14. Wow, that’s so easily in the Xcode14. So now we only need to prepare a few 1024*1024px pictures. Just like this:

image

But if we want to use multiple AppIcons, we need modify the setting in the Build Setting. In the Search Bar filled the text: include all. Then we can see Key: Include All App Icon Assets, make it as Yes.

image

image

Then we begin to write the code. All my code is in the picture. And you can also copy the code directory in the bottom.

Why did I add an alert in the last Text? When an error window pop up, you can customize the message content.

If your system is not in English, you will find that the pop-up message is always in English. So we need to set internationalization. In the TARGETS, then go to Info, add a Key: Localized resources can be mixed, Value: YES.

image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@State var isPresented = false
    var body: some View {
        VStack {
            Text("Default")
                .padding()
                .onTapGesture {
                    UIApplication.shared.setAlternateIconName("hehua")
                }
            Text("egg")
                .padding()
                .onTapGesture {
                    UIApplication.shared.setAlternateIconName("egg")
                }
            Text("devil")
                .padding()
                .onTapGesture {
                    UIApplication.shared.setAlternateIconName("devil")
                }
            Text("arrow")
                .padding()
                .onTapGesture {
                    UIApplication.shared.setAlternateIconName("arrow")
                }
            Text("Error iCon")
                .padding()
                .onTapGesture {
                    UIApplication.shared.setAlternateIconName("errName") { error in
                        isPresented = true
                    }
                }
                .alert(isPresented: $isPresented) {
                    let alert = Alert(title: Text("Error"), message: Text("Because without this Resource, please check"), dismissButton: .default(Text("OK")))
                    return alert
                }
        }
        .padding()
    }
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy