Dismiss all UIAlertControllers currently presented

David picture David · Feb 20, 2015 · Viewed 9.2k times · Source

Is there a way to dismiss all UIAlertControllers that are currently presented?

This is specifically because from anywhere and any state of my app, I need to get to a certain ViewController when a push notification is pressed.

Answer

Agam Mahajan picture Agam Mahajan · Aug 13, 2018
func dismissAnyAlertControllerIfPresent() {
    guard let window :UIWindow = UIApplication.shared.keyWindow , var topVC = window.rootViewController?.presentedViewController else {return}
    while topVC.presentedViewController != nil  {
        topVC = topVC.presentedViewController!
    }
    if topVC.isKind(of: UIAlertController.self) {
        topVC.dismiss(animated: false, completion: nil)
    }
}

This worked for me!