Search code examples
iosswiftuialertviewnotificationcentercontrol-center

Show alertView in appDelegate


I'm trying to add an alertView before the notification/control center is visible with 2 buttons. One button would be "ok" and the notification/control center should then open. The second button should be cancel, and the notification/control center should not be displayed.

I tried adding an alertView in applicationWillResignActive like so:

func applicationWillResignActive(application: UIApplication) {
    var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

When the user tries opening notification/control center, it opens slightly, then closes and the alertView is shown. How can I make the alertView show without the notification/control center opening?


Solution

  • Your AlertView is showing because your app is about to resign active as Notification Center is showing, and you are launching it from the appDelegate method which handles this transition.

    What is it you're trying to accomplish? It is not possible to have your app prevent the Notification Center from showing, or to open it programmatically from the AlertView.

    You can check the iOS application states in table 2-3:

    iOS App Lifecycle