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

To show an alert pop-up that delays a MacOS app from closing (i.e. closing when a user quits), create a custom App Delegate and implement in the App struct.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/18/2024 at 13:18
Last Updated on 10/18/2024 at 13:44
<p>Two flowers that represent SwiftUI and XCode. Beneath them sits the text "Interrupt, Delay or Stop a MacOS app when a user quits."</p>

To show an alert pop-up that delays a MacOS app from closing (i.e. closing when a user quits), create a custom App Delegate and implement in the App struct.

SubscribeDownload Open Source Project

The following tutorial shows code from our upcoming app Object Capture Pro. If you wish to be updated on the progress and release of this app, please subscribe using the link below.

Subscribe

Step One: Create the Delegate

<p>A screenshot of XCode showing the code for the Delegate. Code is found below.</p>

In your app, create a new file called AppDelegate and paste in the code below.

Step Two: Implement the Delegate in the App

<p>A screenshot of XCode showing the App and App Delegate code side by side. Highlighted is how we have implemented the App Delegate in the app. Code below.</p>

Within the main App struct of the project, add the line below to implement the delegate.

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

Step Three: Test

<p>A screenshot of the preliminary design of Object Capture Pro. Above the app sits the alert message asking users if they want to save progress before quiting.</p>

Run the code and attempt to quit the app.

The alert should show up when you do.

Looking to learn more about SwiftUI, Swift, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, design and technology.

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