UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Display warning before closing Window in mac App

Forums > SwiftUI

Hello, everyone. I am in need of some help with a Mac application using SwiftUI. I am currently working on a Mac version of a basic Checkbook Ledger, using a Document-based scheme, and I would like to have my application prompt the user to save, if they have not already done so, when they close the Window.

I tried looking through Internet searches and Apple's documentation, but nothing really seems to fit the bill, or even have any good details on how to implement it properly in the case of NSWindowDelegate.

How do I can I display a prompt kind of like TextEdit does?

My coding environment is XCode 12.5.1 and my target platform is macOS 11. The application itself is a multiplatform app, with a different UI for both iOS and macOS with the main model types being shared between them and the model used has a way to load and save to JSON files.

If you need or want to see the code, it can be viewed here.

2      

You could use two button alert to display the warning to the user

.alert(isPresented: $unsavedChangesToProcess) {
    Alert(title: Text("Close window"),
          message: Text("There are unsaved changes."),
          primaryButton: .default(Text("Proceed"), action: // function call here to close the window / dismiss it),
          secondaryButton: .cancel())

Then unsavedChangesToProcess is either a @State or @ObservedObject variable depending on whether the variable is local to the current view only, or used across several views. It would be a combination of the state of whether there are saved changes and whether the user pressed the close window button.

I think that this approach is in preference to will dismiss method as it is an action before the view dismiss process is set in motion.

2      

I have thought of using an alert like you are suggesting, but the problem is I cannot get the alert to show.

My one idea that I have not been able to try was to reimplement the function called by the Close option in the menu bar, but I could not find anything on it, like I could to replace say the New option in the menu bar.

After that, I started looking into NSWindowDelegate, but, as I mentioned earlier, nobody is discussing how to implement properly in SwiftUI.

I will see if I can implement it in the main view though.

Update: Yeah, that is too simplistic of a method. You are correct that the functionality I want would pertain to a will dismiss kind of method, but an App Delegate, which I have seen people get hooked up in SwiftUI, only deals will instances of terminating an application, not instances of closing a Window, which would fall under NSWindowDelegate and is what I am having troubles with. Without being able to access or implement a windowWillClose method, I do not see how I can set a boolean or an optional that will trigger such an alert.

2      

Ok, after a bit of digging, it looks like my problem was that I was no connecting to the UndoManager in my.

After that, I got something that functions like TextEdit, in that it noticed the document was edited, with no need to implement any delegate.

The following post helped me find where to look:

https://www.hackingwithswift.com/forums/macos/swiftui-app-life-cycle-undo-redo-and-menu-bar-items/7771

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.