Showing alerts in your iOS Apps

Nabin Shrestha
1 min readDec 11, 2020

In our iOS apps, alerts are used in various screens of the app for notifying users for various cases that occurs in app like success, error, information etc.

So, we all know how to create an alert. It is as follows:

A Simple Alert

So, if you use this type of simple alerts in your app in most places with varying title and description. We can move forward two ways instead of creating alert in every ViewController.

  1. Create a BaseViewController.swift and add a func that shows the alert and every other ViewController is subclassed from BaseViewController.
  2. Create a UIViewController extension and add that func there.
  3. Add an AlertWrapper class that takes care of showing alerts.

First Method:

This is fairly simple. You create a function in BaseViewController.swift. For instance, showAlert() and call it from ViewControllers that subclasses it.

Second Method:

In Second Method, we create a class containing extension of UIViewController and add method there.

Third Method:

I found this utility class and it was written in Swift 2.0, all I did was migrated this to work in Swift 5.

That’s it. It usage is also very simple.

That’s it. If you like this article don’t forget to give a clap.

Demo Github Project: https://github.com/nabs107/AlertDemo.git

--

--