SwiftUI.cc
Snippets
PresentationControls ModifiersBeginner

SwiftUI Alerts and Confirmation Dialogs

Use alerts for urgent decisions and confirmationDialog for action choices that can adapt across iPhone, iPad, and macOS.

4 min readUpdated 2026-06
Delete confirmation
import SwiftUI

struct DeleteProjectButton: View {
    @State private var showingDialog = false

    var body: some View {
        Button("Delete Project", role: .destructive) {
            showingDialog = true
        }
        .confirmationDialog("Delete this project?", isPresented: $showingDialog) {
            Button("Delete", role: .destructive) { deleteProject() }
            Button("Cancel", role: .cancel) {}
        } message: {
            Text("This removes local drafts and cannot be undone.")
        }
    }
}
Use this when

A user action needs a clear yes/no confirmation before continuing.

A destructive action needs platform-native emphasis and ordering.

A button should offer several related actions without navigating away.

Avoid this when

The content is long or requires scrolling; use a sheet instead.

The choice is not important enough to interrupt the current workflow.

You are using an alert as a substitute for inline validation.

Implementation notes

Button roles are more than color; the system can use them for placement and emphasis.
Use alerts for a compact decision and confirmation dialogs for choosing among actions.
On compact devices, dialogs adapt differently from larger screens, so test both sizes.

Checklist

Keep the title short and action-oriented.

Use role: .destructive only for irreversible or risky actions.

Make sure there is always a safe cancellation path.

Related reference