Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Last active September 29, 2024 15:39
Show Gist options
  • Save lucianoschillagi/765e8f3601447e7ea736f37a61807cec to your computer and use it in GitHub Desktop.
Save lucianoschillagi/765e8f3601447e7ea736f37a61807cec to your computer and use it in GitHub Desktop.
Application will terminate method (SwiftUI integration)
/*
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