swiftUI detects app state moves to foreground from background

1.4k views Asked by At

I have below logic which give app state: But i wanted to add logic when app move to foreground.

In Swift we have method. I am looking for something similar method in swiftui

func sceneWillEnterForeground(_ scene: UIScene) {

 

but in Swiftui i only have .active, .inactive and .background


     .onChange(of: scenePhase) { newPhase in
                if newPhase == .active {
                    
                } else if newPhase == .inactive {
                    
                } else if newPhase == .background {
                    
                }
            }
1

There are 1 answers

0
Asperi On

It is possible to use .onReceive with subscription to Notification Center publisher on UIScene.willEnterForegroundNotification notification, or, after all, mentioned scene delegate callback (having scene delegate as described in https://stackoverflow.com/a/60359809/12299030)