How to execute functions when a user quits a SwiftUI app
Listen for the willTerminateNotification within a view of a iOS, iPadOS, MacOS, TVOS, VisionOS or WatchOS app to carry out actions when the app exits or closes.
The following tutorial has been made available through the tutorials/phases/terminate-notification branch on our Open Source Swift Starter Project.
To listen for when a user quits an app, use the functionality below.
Please note this receiver can only be attached to a view.
Multi-Platform
AppKit (MacOS Only)
If you are seeking to learn how to present an alert (pop-up) when a user quits a MacOS app, consult the tutorial below.
Looking to learn more about things you can do with Swift, SwiftUI or XCode ?
Search our blog to find educational content on learning how to use Swift, SwiftUI or XCode.