Skip to content

Instantly share code, notes, and snippets.

@EvolvingParty
Created November 19, 2022 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvolvingParty/d783c1265ccbf247e363aa015f13abb0 to your computer and use it in GitHub Desktop.
Save EvolvingParty/d783c1265ccbf247e363aa015f13abb0 to your computer and use it in GitHub Desktop.
How to detect if the SwiftUI application in background or foreground
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