Skip to content

Instantly share code, notes, and snippets.

@EvolvingParty
Created November 19, 2022 23:20
Embed
What would you like to do?
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