Instance Property

showsSuppressionButton

Specifies whether the alert includes a suppression checkbox, which you can employ to allow a user to opt out of seeing the alert again.

Declaration

var showsSuppressionButton: Bool { get set }

Discussion

The default value of this property is false, which specifies the absence of a suppression checkbox in the alert. Set the value to true to show a suppression checkbox in the alert.

By default, a suppression checkbox has the title, “Do not show this message again.” To customize it, use the checkbox’s title property, as follows:

myAlert.suppressionButton.title = @"Do not show this warning again";

To create an alert that responds to the selection state of the suppression checkbox, use code like that shown in Listing 1. Figure 1 shows the corresponding alert.

Listing 2

Creating an alert with a suppression checkbox

NSString *alertSuppressionKey = @"AlertSuppression";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 
if ([defaults boolForKey: alertSuppressionKey]) {
    NSLog (@"Alert suppressed");
} else {
    NSAlert *anAlert = [[NSAlert alloc] init];
    anAlert.messageText = @"Message text.";
    anAlert.informativeText = @"Informative text.";
    anAlert.showsSuppressionButton = YES; // Uses default checkbox title
    [anAlert runModal];
    if (anAlert.suppressionButton.state == NSOnState) {
        // Suppress this alert from now on
        [defaults setBool: YES forKey: alertSuppressionKey];
    }
}
Figure 1

Alert with a suppression checkbox and customized text

See Also

Displaying Alerts

func runModal() -> NSApplication.ModalResponse

Runs the alert as an app-modal dialog and returns the constant positionally identifying the button clicked.

func beginSheetModal(for: NSWindow, modalDelegate: Any?, didEnd: Selector?, contextInfo: UnsafeMutableRawPointer?)

Runs the alert modally as an alert sheet attached to a specified window.

Deprecated
var suppressionButton: NSButton?

The alert’s suppression checkbox.