Article

Getting the User's Attention with Alerts and Action Sheets

Present important information to the user or prompt the user about an important choice.

Overview

Display an alert or action sheet when your app requires additional information or acknowledgment from the user. Alerts and action sheets interrupt your app’s normal flow to display a message to the user. In Figure 1, the left image shows an alert and the right image shows an action sheet. The user dismisses an alert or action sheet by selecting one of the listed options.

Figure 1

Alerts and action sheets

An alert shows location services are off.  An action sheet from Safari asks what to do with tabs.

To display an alert or action sheet, create a UIAlertController object, configure it, and call its present(_:animated:completion:) method, as shown in Listing 1. Configuring the alert controller includes specifying the title and message that you want the user to see and the actions the user can select. You must add at least one action—represented by a UIAlertAction object—to an alert controller before presenting it.

Listing 1

Displaying an alert with two actions

@IBAction func agreeToTerms() {
   // Create the action buttons for the alert.
   let defaultAction = UIAlertAction(title: "Agree", 
                        style: .default) { (action) in
	// Respond to user selection of the action.
   }
   let cancelAction = UIAlertAction(title: "Disagree", 
                        style: .cancel) { (action) in
	// Respond to user selection of the action.
   }
   
   // Create and configure the alert controller.     
   let alert = UIAlertController(title: "Terms and Conditions",
         message: "Click Agree to accept the terms and conditions.",
         preferredStyle: .alert)
   alert.addAction(defaultAction)
   alert.addAction(cancelAction)
        
   self.present(alert, animated: true) {
      // The alert was presented
   }
}

Present an Action Sheet on iPad

On iPad, UIKit requires that you display an action sheet inside a popover. Figure 2 shows an action sheet anchored to a bar button item.

Figure 2

Action sheets in popovers

On iPad, an action sheet is anchored to a bar button item in a toolbar.

To display your action sheet in a popover, specify your popover’s anchor point using the popoverPresentationController property of your alert controller. It is safe to configure this property regardless of the underlying device. In other words, Listing 2 displays the action sheet in a popover on iPad and as a slide-up presentation on iPhone.

Listing 2

Presenting an action sheet on iPad and iPhone

@IBAction func deleteItem() {
   let destroyAction = UIAlertAction(title: "Delete", 
             style: .destructive) { (action) in
	// Respond to user selection of the action
   }
   let cancelAction = UIAlertAction(title: "Cancel", 
             style: .cancel) { (action) in
	// Respond to user selection of the action
   }
        
   let alert = UIAlertController(title: "Delete the image?", 
               message: "", 
               preferredStyle: .actionSheet)
   alert.addAction(destroyAction)
   alert.addAction(cancelAction)
        
   // On iPad, action sheets must be presented from a popover.
   alert.popoverPresentationController?.barButtonItem = 
               self.trashButton
        
   self.present(alert, animated: true) {
      // The alert was presented
   }
}

See Also

Alerts

class UIAlertController

An object that displays an alert message to the user.

class UIAlertAction

An action that can be taken when the user taps a button in an alert.