24.5 ScenePhase and the onChange Modifier
ScenePhase is an @Environment property which is used by SwiftUI to store the state of the current scene. When changes to ScenePhase are monitored by the onChange() modifier, an app is able to take action, for example, when the scene moves between the foreground and background or when it becomes active or inactive. This technique can be used on any view or scene, but is also useful when applied to the App declaration. For example, edit the LifecycleDemoApp.swift file and modify it so that it reads as follows:
import SwiftUI
@main
struct LifecycleDemoApp: App {
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
...