Presenting alerts
A common way of showing important information to users is by using alerts with a message and some buttons. In this recipe, we will create a simple alert that gets displayed when a button is pressed.
The way alerts are presented in iOS 13 and 14 has been deprecated. iOS 15 introduced a new way of presenting alerts. For the sake of being able to handle devices on earlier iOS versions, we'll go over both ways of presenting alerts, starting with iOS 15. The code for iOS 13 and 14 alerts is found in the oldAlerts.swift
file while the newer iOS 15 version is located in ContentView.swift
.
Getting ready
Create a new SwiftUI project called PresentingAlerts
.
Set the project's iOS Deployment Target to iOS 15.0.
How to do it
We will add a Button
or Text
to the scene that can be used to display an alert containing a single button.
The iOS 15 steps are as follows:
- Create a
@State
variable that will be used to trigger the presentation of our...