How to show Alert Dialog in iOS?

Knowing how to play with Alert is very important if you’re designing any iOS Application. Here we will be focusing on how to show Alert using UIAlertController.

To read more about UIAlertController refer −

In this, we will be creating a new project where we will have a button, on tapping that button we will show alert with custom message.

Step 1 − Open Xcode → New Projecr → Single View Application → Let’s name it “Alert”

Step 2 − Open Main.storyboard and add a button and name it tap. Create @IBAction of that button in ViewController.swit and name the same as tap.

There’s 3 steps to show the alert. First is to create alert object from UIAlertController. Second add action to alert object and lastly present the alert object.

Step 3 − Add the below code in your button instance, that is under you @IBAction of tap button.

@IBAction func tap(_ sender: Any) {
   let uialert = UIAlertController(title: "Welcome", message: "Welcome to my channel. Thanks for watching.
   Click on Okay to continue", preferredStyle: UIAlertController.Style.alert)
      uialert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil))
   self.present(uialert, animated: true, completion: nil)

Step 4 − And Run the code.