Alerting the user as to a particular situation is done with, well, an alert box.
There are three fundamental steps to setting up an alert box:
- defining the alert
- adding an action request, and
- presenting the alert to the user.
This is shown in the code which follows.
// Step one: create the alert and specify basic information:
let alert = UIAlertController(
title: "Title for the alert box",
message: "Message outlining the nature of the alert",
preferredStyle: .alert)
// Step two: add instructions to the user. Note that if this is
// omitted, the box will still display but it will not be possible to
// dismiss it by tapping outside the box.
alert.addAction(
UIAlertAction(
title: "Remedial action to be taken",
style: .default,
handler: nil) )
// Step three: present the alert and await the user's response.
present( alert, animated: true, completion: nil )