SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

November 11, 2025·1 min read·by dockui

SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

Deleting data, overwriting a file, leaving a group, resetting settings — these should require an explicit “Yes, I mean it.” SwiftUI gives us this structure with the built-in .alert modifier.

Implementation

Code Snippet

You show the alert by toggling a boolean.
SwiftUI handles the presentation lifecycle for you.

Destructive variant (when the action is dangerous)

Use role: .destructive when the user is about to lose something.

Code Snippet

Apple’s HIG expects this.
Users instantly understand red = irreversible.

Pairing confirmation with haptics (good UX)

Good mobile apps give “feel” to the user’s confirmation. One line:

Code Snippet

You can fire that inside your confirm callback.

Best practices for developers

  • always require confirmation before deleting something
  • make the title of the alert specific (“Delete photo?” not “Are you sure?”)
  • avoid alerts for navigation — alerts are for commit actions

Similar Blogs

View All Articles
SwiftUI: IOS Haptic Feedback Guide 2026

SwiftUI: IOS Haptic Feedback Guide 2026

Add click to vibrate haptic feedback in SwiftUI. Full guide to UIImpactFeedbackgenerator, success, error, selection, and tap vibration patterns with code.

by dockui
Nov 11
SwiftUI: How To Reset a Form

SwiftUI: How To Reset a Form

Learn how to reset a SwiftUI form with one line. Clear text fields instantly using @State or a form model. Simple copy/paste SwiftUI code examples.

by dockui
Nov 10
SwiftUI: How To Toggle a Switch Programmatically

SwiftUI: How To Toggle a Switch Programmatically

Learn how to toggle a SwiftUI switch programmatically with @State and @Binding. Simple examples with code to trigger Toggle from buttons or other views.

by dockui
Nov 10
Animate Number Changes in SwiftUI

Animate Number Changes in SwiftUI

Smoothly animate number changes in SwiftUI. Use .contentTransition(.numericText()) on iOS 17+, with clean older fallbacks for pre-17 devices.

by dockui
Nov 7

Code copied to clipboard!