How to detect if the SwiftUI application in background or foreground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@main | |
struct ios14_demoApp: App { | |
@Environment(\.scenePhase) var scenePhase | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
}.onChange(of: scenePhase) { phase in | |
switch phase { | |
case .background: | |
print("App is in background") | |
case .active: | |
print("App is Active") | |
case .inactive: | |
print("App is Inactive") | |
@unknown default: | |
print("New App state not yet introduced") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment