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

Oscar de la Hera Gomez
Two flowers that represent SwiftUI and XCode. Beneath them sits the text "Interrupt, Delay or Stop a MacOS app when a user quits."

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.

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.

Step One: Create the Delegate

A screenshot of XCode showing the code for the Delegate. Code is found below.

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

Step Two: Implement the Delegate in the App

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.

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

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

Step Three: Test

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.

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.

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