One Logo Pool Ball
SwiftUI · IOS API REFERENCE

Alert

struct · iOS 13.0+ · Deprecated in iOS 100000.0 · SDK 27.1

struct Alert

A representation of an alert presentation. Use an alert when you want the user to act in response to the state of the app or the system. If you want the user to make a choice in response to their own action, use an ``ActionSheet`` instead. You show an alert by using the ``View/alert(isPresented:content:)`` view modifier to create an alert, which then appears whenever the bound `isPresented` value is `true`. The `content` closure you provide to this modifer produces a customized instance of the `Alert` type. In the following example, a button presents a simple alert when tapped, by updating a local `showAlert` property that binds to the alert. @State private var showAlert = false var body: some View { Button("Tap to show alert") { showAlert = true } .alert(isPresented: $showAlert) { Alert( title: Text("Current Location Not Available"), message: Text("Your current location can’t be " + "determined at this time.") ) } } ![A default alert dialog with the title Current Location Not Available in bold text, the message your current location can’t be determined at this time in smaller text, and a default OK button.](SwiftUI-Alert-OK.png) To customize the alert, add instances of the ``Alert/Button`` type, which provides standardized buttons for common tasks like canceling and performing destructive actions. The following example uses two buttons: a default button labeled "Try Again" that calls a `saveWorkoutData` method, and a "destructive" button that calls a `deleteWorkoutData` method. @State private var showAlert = false var body: some View { Button("Tap to show alert") { showAlert = true } .alert(isPresented: $showAlert) { Alert( title: Text("Unable to Save Workout Data"), message: Text("The connection to the server was lost."), primaryButton: .default( Text("Try Again"), action: saveWorkoutData ), secondaryButton: .destructive( Text("Delete"), action: deleteWorkoutData ) ) } } ![An alert dialog with the title, Unable to Save Workout Data in bold text, and the message, The connection to the server was lost, in smaller text. Below the text, two buttons: a default button with Try Again in blue text, and a button with Delete in red text.](SwiftUI-Alert-default-and-destructive.png) The alert handles its own dismissal when the user taps one of the buttons in the alert, by setting the bound `isPresented` value back to `false`.

Members (2)

init · iOS 13.0+ · Deprecated in iOS 100000.0

init

TypeScript
static initTitleMessageDismissButton(title: _SwiftUICore_Text, message?: (_SwiftUICore_Text) | null, dismissButton?: (Alert_Button) | null): Alert
Swift declaration
init(title: Text, message: Text? = nil, dismissButton: Alert.Button? = nil)

Creates an alert with one button. - Parameters: - title: The title of the alert. - message: The message to display in the body of the alert. - dismissButton: The button that dismisses the alert.

init · iOS 13.0+ · Deprecated in iOS 100000.0

init

TypeScript
static initTitleMessagePrimaryButtonSecondaryButton(title: _SwiftUICore_Text, message: (_SwiftUICore_Text) | null, primaryButton: Alert_Button, secondaryButton: Alert_Button): Alert
Swift declaration
init(title: Text, message: Text? = nil, primaryButton: Alert.Button, secondaryButton: Alert.Button)

Creates an alert with two buttons. The system determines the visual ordering of the buttons. - Parameters: - title: The title of the alert. - message: The message to display in the body of the alert. - primaryButton: The first button to show in the alert. - secondaryButton: The second button to show in the alert.

Skipped declarations

No declarations were skipped in this snapshot.

All SwiftUI types →