How to execute functions when a user quits a SwiftUI app

Oscar de la Hera Gomez
Two flowers that represent SwiftUI and XCode side by side. Beneath them sits the tex "App will terminate notification."

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.

A screenshot of XCode showing how the breakpoint is triggered within the closure that listens for the willTerminateNotification when a user quits an app.

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.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details