Skip to content

SwiftUI: Alert

Alert provides a pop up that appears in the center of the device which could either contain message with button that will trigger based on your need or message and a button that acknowledge the message. Either way, it should work to fit your need.

A container for an alert presentation.

Apple Documentation

In this tutorial, you’ll learn what is alert in SwiftUI. You’ll learn:

  • How to create an alert

Prerequisites

To follow along this tutorial, you’ll need some basic knowledge in:

  • A basic familiarity with Swift.
  • At least Xcode 11

Alert

Using a flag to determine whether the alert is shown or not.

Here, you will be using a button that will trigger an alert.

With the code above, by clicking on the button, an alert will then pop up. You may configure the title, message and button. For the current configuration, only 1 button is configure.

If there is a need for more than one button, you may configure to your need.

Where to go From Here

If you feel like you are ready to take on more challenges, feel free to check out some other tutorials that we have created:

Published inSwiftUI

One Comment

  1. Brian Brian

    The problem here is that one must click a button to display the alert. What if I want to display the alert programmatically when some event happens. for example, I discover a Bluetooth device? The alert tells the user info about the device and asks if a connection is desired, If a YES is clicked, a BLuetooth method is called to connect to the device.

Leave a Reply

Your email address will not be published. Required fields are marked *