Category

NSAlert: Don’t show this again

Swift Language

How to let the user choose to not see an alert

Do you really want to do this? Are you sure? Are you really sure?

Annoying.

So, how do you let the user stop this crazy thing?

(And also, how do you prevent a window from being closed?)

((And) (also “Also how do you ask if they really want to quit the program?”))

(hey “I like Lots of Irritating Stupid Parentheses”)

Introduction

Table of Contents

Let’s say you don’t want the user to close the application window by clicking the red dot in the title bar. You’re a control freak, right? I said use the Quit menu item!

Give the window a delegate, and implement windowShouldClose to return false. Easy.
In this example, I created a NSWindowController (and set it on the window controller in the storyboard) as a window delegate – and did other window controller frobs that I’m not showing here.

So the user keeps mashing the red button. “Why isn’t this #$(*^#ing window closing?!?”
Show them an alert to tell them you’re not playing their game.

Now, every time they hit the red button they get the alert. You may want that just to be annoying. If you’re from Brooklyn, that’s a definite yes, amirite?

Well, the NSAlert class has this gem:

You set that property, and a check box with a default message will appear in the alert.

What if the user checks it? How do you handle this?

After runModal() returns, get the suppressionButton. It’s a control, so it has a state property. Simply inspect that value and save it for the next time. UserDefaults is a good place. Then, the next time the alert is about to be shown, check UserDefaults to get the user’s preference.

Easy once you see it.

The whole thing:

Bonus! Do you really want to quit?

Table of Contents

The user chooses Quit from the file menu or types ⌘-Q.

Some developers are courteous enough to ask if they really want to quit.
And then there are users who don’t want to be asked.
You know what to do now with the alert!

But, you will find a lot of wrong answers on StackOverblown for how to catch this event.
Look at the quit button in IB. It’s connected to terminate: on the responder chain. NSApplication has this method, so you probably don’t want to write your own. Take a look at the documentation (link below). When its terminate: is called it does a lot before terminating. One thing it does is call the application delegate’s applicationShouldTerminate: method.

So that’s what I’m doing here.
Also, as a bonus bonus, I’m changing the suppression message just to show how.

Summary

Table of Contents

You smart now.
Ask for a raise.

Resources

Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.