Apple Developer Documentation

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

https://docs-assets.developer.apple.com/published/9a34a8af4d/a614c5a1-d46b-4359-802e-1f70f360128b.png

Important

Because alerts and action sheets are an interruption, use them sparingly and only when absolutely needed. For detailed guidance on when to use them, see iOS Human Interface Guidelines.


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.

@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