Created
April 7, 2023 18:47
-
-
Save AlexZverusha/180c95b9c4c757a08fb14a850e68eaa0 to your computer and use it in GitHub Desktop.
SwiftUI app life cycle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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