Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling close and terminate app events (SwiftUI)

How can I handle close and terminate app events in SwiftUI?

View
{
    ...
}.onDisappear {
    //My code
}

Working only when I change view, not when I close or terminate my app.

like image 970
alexbayker Avatar asked Dec 22 '25 12:12

alexbayker


1 Answers

You can use UIApplication.willTerminateNotification:

NotificationCenter.default.addObserver(forName: UIApplication.willTerminateNotification, object: nil, queue: .main) { _ in
    // terminating
}

This answer may explain better how to use it in SwiftUI:

  • How can I use a method without any page transition or any reboot app
like image 163
pawello2222 Avatar answered Dec 24 '25 01:12

pawello2222



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!