0

How can I prevent a simple NSAlert from being dismissed?

F.ex., when the application launches, I show a NSAlert with a NSTextField included. The user shall type in a password. Only if the password is correct, the user should be allowed to use the application, if not (if the password is not correct), the Alert should stay there and ask again for the password.

This is my code so far (to create the alert):

func applicationDidFinishLaunching(_ aNotification: Notification){
    let alert = NSAlert()
    alert.addButton(withTitle: "Send")
    alert.delegate = self
    alert.alertStyle = .informational
    alert.messageText = "Password - Login"
    alert.informativeText = "Please type in your password: "

    let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
    txt.stringValue = "Password:"


    alert.accessoryView = txt
    alert.beginSheetModal(for: NSApplication.shared().mainWindow!) { (response) in
        if (response == NSAlertFirstButtonReturn) {
            // the alert closes here, is there any way to prevent this?
        } else {
            print("No value.")
        }
    }

OS: OS X Sierra, Swift 3

j3141592653589793238
  • 1,810
  • 2
  • 16
  • 38

1 Answers1

1

You can present the alert a second time; if you need to customize the behavior beyond that, you'll need to eschew NSAlert and run an NSWindow or NSPanel of your own making.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • Thanks, I'll try out making my own "alert" with a NSPanel. Btw, do you have an idea why my question is so down voted? Is it too short, should I add detail? – j3141592653589793238 Aug 16 '17 at 16:44
  • 1
    I'm not one of the people who downvoted it, but the question is quite short. It is, in general, better to be more clear and provide more detail about exactly what you are trying to do. – Charles Srstka Aug 16 '17 at 16:45
  • Ok, thank you. I really didn't mean to annoy somebody or so. I'll edit it and try to make it more precise. Thanks. – j3141592653589793238 Aug 16 '17 at 16:48