Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexZverusha/180c95b9c4c757a08fb14a850e68eaa0 to your computer and use it in GitHub Desktop.
Save AlexZverusha/180c95b9c4c757a08fb14a850e68eaa0 to your computer and use it in GitHub Desktop.
SwiftUI app life cycle
import SwiftUI
@main
struct testSwitUIApp: App {
let persistenceController = PersistenceController.shared
@Environment (\.scenePhase) private var scenePhase
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
init() {
print("init app........")
}
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
.onChange(of: scenePhase) { phase in
if phase == .background {
print("phase: .background")
}
if phase == .active {
print("phase: .active")
appDelegate.doSmth()
}
if phase == .inactive {
print("phase: .inactive")
}
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
func doSmth() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment