1
votes

Since iOS 8 and the new UIAlertController, in a scenario where:

  • A UIAlertController is presented.
  • A local push notification banner is displayed on top, as a UIWindow.
  • User taps on the banner and then navigated to a different UIViewController and the UIAlertController is being dismissed by the window.rootViewController...

Is there a way to detect such dismissal that is not activated by any of the UIAlertAction buttons?

1

1 Answers

0
votes

To avoid that your UIAlertViewController is dismissed by new UIViewController, my solution is: - Create new UIWindow with level = UIWindowALertLEvel +1 - add empty rootViewController for that UIWindow - Make that UIWIndow a keyWindow - show alertController from the rootViewController.

So, this alertcontroller won't not be dismissed by another viewcontroller.

My code:

func showSimpleAlertOverWindow(title: String, msg: String, okButtonTitle : String, animated : Bool) {
        CLWrapper.logDebug("show message <\(msg)>")

        let _alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
        _alertWindow.rootViewController = UIViewController()
        _alertWindow.windowLevel = UIWindowLevelAlert + 1
        _alertWindow.hidden = false

        let alert = UIAlertController(title: title ?? "", message: msg ?? "", preferredStyle: UIAlertControllerStyle.Alert)
        let okBtn = UIAlertAction(title: okButtonTitle ?? "", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
            _alertWindow.resignKeyWindow()
            _alertWindow.hidden = true

        }

        alert.addAction(okBtn)

        _alertWindow.makeKeyWindow()
        _alertWindow.rootViewController!.presentViewController(alert, animated: animated, completion: nil)

    }