Skip to content

Display Alert Controller Popup from any UIViewController (iOS/Swift) Extension

Displaying an alert from an Extension

Displaying an alert from an ExtensionMany apps use the UIAlertController to display information the user for them to acknowledge with an “OK” button. A UIViewController needs to present the alert controller to the user so I wrote an extension on UIViewController to handle this.

This is just a simple alert to the user with an “OK” button that dismisses the alert popup. It doesn’t provide the user with

This extension has a function that takes a title string, a message string, they style of the alert (alert or action sheet) and optionally a “Don’t Remind” key.

The “Don’t Remind” key is a string that’s used as a key for UserDefaults. If this key is provided, the alert controller will include a “Don’t Remind” button on the popup. Then next time this function is called with that key, the value in UserDefaults is checked. If the user previously tapped, this button it won’t be shown again.

 

Display Alert Controller from a UIViewController Extension

 

This is good for introductory information or certain warnings the user may not care about.

Here’s the extension:

extension UIViewController {
    func displayMsg(title : String?, msg : String, 
          style: UIAlertControllerStyle = .alert, 
          dontRemindKey : String? = nil) {
        if dontRemindKey != nil, 
           UserDefaults.standard.bool(forKey: dontRemindKey!) == true {
            return
        }
        
        let ac = UIAlertController.init(title: title, 
                   message: msg, preferredStyle: style)
        ac.addAction(UIAlertAction.init(title: "OK", 
            style: .default, handler: nil))
        
        if dontRemindKey != nil {
            ac.addAction(UIAlertAction.init(title: "Don't Remind", 
             style: .default, handler: { (aa) in
                UserDefaults.standard.set(true, forKey: dontRemindKey!)
                UserDefaults.standard.synchronize()
            }))
        }
        DispatchQueue.main.async {
            self.present(ac, animated: true, completion: nil)
        }
    }
}

You can see that there’s just one function and it’s parameters. Note it’s presenting the alert controller on the main thread.

Don’t Remind Me

If the dontRemindKey is passed in, it’s check for true in UserDefaults. If it’s true, we’re done: return

Otherwise, create the alert controller and add the “OK” action (with no handler closure). If the dontRemindKey is not nil, create the action for that also and in the handler set the UserDefaults value to true for that key (and synchronize to store the value now).

This is how it may be called from a UIViewController instance:

displayMsg(title: "Title", msg: "Some text for a message", style: .alert, dontRemindKey: "UserHasSeenMsg")

 

Since the alert action handles the callback when the user taps a button, there’s nothing else to do.

If you have a way of doing this, please share it in the comments. Thanks!