Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

swiftui onappear only once

If you're linking against iOS15 then you can take advantage of the new scenePhase concept:

@Environment(.scenePhase) var scenePhase

Where ever you are the Environment if injecting this property that you can test against three conditions:

switch newPhase {
    case .inactive:
        print("inactive")
    case .active:
        print("active")
    case .background:
        print("background")
}

So, all together:

struct ContentView: View {
    @Environment(.scenePhase) var scenePhase

    var body: some View {
        Text("Hello, World!")
            .onChange(of: scenePhase) { newPhase in
                switch newPhase {
                    case .inactive:
                        print("inactive")
                    case .active:
                        print("active")
                    case .background:
                        print("background")
                }
            }
    }
}
 
PREVIOUS NEXT
Tagged: #swiftui #onappear
ADD COMMENT
Topic
Name
6+4 =