// // SwiftUIDemoApp.swift // SwiftUIDemo // // Created by shiyanjun on 2023/1/29. // import SwiftUI @main struct LifeCycleApp: App { // 场景阶段环境变量 @Environment(\.scenePhase) var scenePhase var body: some Scene { WindowGroup { ContentView() } .onChange(of: scenePhase) { phase in switch phase { case .background: // 应用在后台运行状态 print("background") case .inactive: // 应用处于不活动状态 print("inactive") case .active: // 应用处于活动状态 print("active") @unknown default: // 应用处于默认状态 print("default") } } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter