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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/18/2024 at 12:21
Last Updated on 10/18/2024 at 13:45
<p>Two flowers that represent SwiftUI and XCode side by side. Beneath them sits the tex "App will terminate notification."</p>

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.

SubscribeDownload Open Source Project

The following tutorial has been made available through the tutorials/phases/terminate-notification branch on our Open Source Swift Starter Project.

View Repository
<p>A screenshot of XCode showing how the breakpoint is triggered within the closure that listens for the willTerminateNotification when a user quits an app.</p>

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.

How to interrupt, delay or stop a MacOS SwiftUI App from terminating

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.

Search our Blog

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe