In iPhone apps, there comes a time when you might need to show an alert to a user to ask them for input. Apple provides the alert instance method on a view for you to use. This particular instance method is for iOS 15 and later. Here is how you use it:
struct ContentView: View {
@State private var showAlert = false
var body: some View {
VStack {
Button("Show Alert") {
showAlert.toggle()
}
.alert(
"Important Message", // Title of the alert
isPresented: $showAlert, // Binding to show the alert
actions: {
Button("Allow") {
// Action when "Allow" button is pressed
print("User pressed Allow")
}
Button("Don't Allow", role: .destructive) {
// Action when "Don't Allow" button is pressed
print("User pressed Don't Allow")
}
},
message: {
Text("Do you want to allow this action?")
}
)
.padding()
}
}
}