Using UIAlertController - codepath/ios_guides GitHub Wiki

You can choose whether to display as an alert or action sheet style.

Using Alert style

The alert style presents modally in the center of their presenting view controllers.

Alert|250

Step 1: Create the UIAlertController

Create the alert controller as below, setting preferredStyle to .alert:

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                           message:@"Message"
                                                                    preferredStyle:(UIAlertControllerStyleAlert)];

Step 2: Add buttons

Create a UIAlertAction for each button to display and respond to.

// create a cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
    // handle cancel response here. Doing nothing will dismiss the view.
}
// add the cancel action to the alertController
alertController.addAction(cancelAction)

// create an OK action
let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in
    // handle response here.
}
// add the OK action to the alert controller
alertController.addAction(OKAction)
// create a cancel action
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                    style:UIAlertActionStyleCancel
                                                  handler:^(UIAlertAction * _Nonnull action) {
                                                         // handle cancel response here. Doing nothing will dismiss the view.
                                                  }];
// add the cancel action to the alertController
[alert addAction:cancelAction];

// create an OK action
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                   style:UIAlertActionStyleDefault
                                                 handler:^(UIAlertAction * _Nonnull action) {
                                                         // handle response here.
                                                 }];
// add the OK action to the alert controller
[alert addAction:okAction];

Adding two buttons will place them side by side in the alert. Adding more than two buttons stacks them in the view similar to the ActionSheet style.

Step 3: Show the UIAlertController

present(alertController, animated: true) {
    // optional code for what happens after the alert controller has finished presenting
}
[self presentViewController:alert animated:YES completion:^{
    // optional code for what happens after the alert controller has finished presenting
}];

If you wish to call present() outside a view controller, you would do:

// UIApplication.shared.keyWindow is deprecated in iOS 13+. Since iOS 13, windows belong to a
// UIWindowScene, so find the foreground-active scene and use its key window (UIWindowScene.keyWindow
// requires iOS 15+; for iOS 13/14, replace `?.keyWindow` with `?.windows.first(where: { $0.isKeyWindow })`).
UIApplication.shared.connectedScenes
    .compactMap { $0 as? UIWindowScene }
    .first { $0.activationState == .foregroundActive }?
    .keyWindow?.rootViewController?.present(alertController, animated: true) {
        // optional code for what happens after the alert controller has finished presenting
    }
// UIApplication.sharedApplication.keyWindow is deprecated in iOS 13+. connectedScenes is a
// NSSet with no guaranteed ordering, so filter for the foreground-active scene explicitly.
UIWindowScene *windowScene = nil;
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
    if ([scene isKindOfClass:[UIWindowScene class]] &&
        scene.activationState == UISceneActivationStateForegroundActive) {
        windowScene = (UIWindowScene *)scene;
        break;
    }
}
[windowScene.keyWindow.rootViewController presentViewController:alert
                                                       animated:YES
                                                     completion:^{
                                                         // optional code for what happens after the alert controller has finished presenting
                                                     }];

Using Action Sheet style

The action sheet style anchors the view to the bottom of the presenting view controller. On iPad, UIKit shows an action sheet as a popover instead, and a popover needs an anchor location that you provide (see Step 3).

Action|250

Step 1: Create the UIAlertController

Create the alert controller as below, setting preferredStyle to .actionSheet:

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)

Step 2: Add buttons

Create a UIAlertAction for each button to display and respond to. The destructive style shows the button text in red.

let logoutAction = UIAlertAction(title: "Log Out", style: .destructive) { (action) in
    // handle case of user logging out
}
// add the logout action to the alert controller
alertController.addAction(logoutAction)     

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
    // handle case of user canceling. Doing nothing will dismiss the view.
}
// add the cancel action to the alert controller
alertController.addAction(cancelAction)

Step 3: Anchor and show the UIAlertController

When UIKit shows an action sheet as a popover, as it does on iPad, it needs to know where to point the popover. Set the anchor on the alert controller's popoverPresentationController before calling present(_:animated:completion:). Use either sourceView together with sourceRect, or sourceItem (available since iOS 16). If you present an action sheet on iPad without an anchor, the app crashes with an exception that begins "You must provide location information for this popover through the alert controller's popoverPresentationController" (Apple Developer Forums).

The example below shows the action sheet from a button's action method and anchors it to the tapped button:

@IBAction func logOutTapped(_ sender: UIButton) {
    // ... create alertController and add its actions as in Steps 1 and 2 ...

    // Anchor the popover to the tapped button.
    alertController.popoverPresentationController?.sourceView = sender
    alertController.popoverPresentationController?.sourceRect = sender.bounds

    present(alertController, animated: true) {
        // optional code for what happens after the alert controller has finished presenting
    }
}

Alternatively, replace the two anchor lines with a single sourceItem assignment. sourceItem takes any type that conforms to UIPopoverPresentationControllerSourceItem, and Apple's documentation lists both UIView (so UIButton too) and UIBarButtonItem among the conforming types:

alertController.popoverPresentationController?.sourceItem = sender

To anchor to a bar button item, use sourceItem rather than the older barButtonItem property, which Apple's documentation marks as deprecated in iOS 27.