Last active
September 29, 2024 15:39
-
-
Save lucianoschillagi/765e8f3601447e7ea736f37a61807cec to your computer and use it in GitHub Desktop.
Application will terminate method (SwiftUI integration)
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
| /* | |
| If you need more control over app termination, you can integrate UIApplicationDelegate methods within SwiftUI. | |
| In particular, applicationWillTerminate gets called right before the app is terminated, | |
| but it won’t be called if the user force quits the app. | |
| To use this in SwiftUI: | |
| Create an AppDelegate class that conforms to UIApplicationDelegate. | |
| Use UIApplicationDelegateAdaptor to link it to your SwiftUI app. | |
| */ | |
| import SwiftUI | |
| class AppDelegate: NSObject, UIApplicationDelegate { | |
| func applicationWillTerminate(_ application: UIApplication) { | |
| // Code to execute when the app is about to terminate | |
| print("App is about to terminate") | |
| } | |
| } | |
| @main | |
| struct MyApp: App { | |
| // Connect the AppDelegate to SwiftUI app | |
| @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate | |
| var body: some Scene { | |
| WindowGroup { | |
| ContentView() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment