Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show UIAlertController from Appdelegate

I'm working with PushNotification on iOS app. I would like to show a UIalertcontroller when the app receive a notification.

I try this code below in the AppDelegate:

[self.window.rootViewController presentViewController:alert animated:YES completion:nil]; 

But the UIAlertcontroller is showing in the root View (First screen) and for other uiviewcontroller i got warning or the app crashes.

like image 269
Rockers23 Avatar asked Mar 22 '16 13:03

Rockers23


People also ask

How do I show notifications in delegate app?

@OrkhanAlizade create a ViewController , put your code into the ViewControllers viewDidAppear method, and in your AppDelegate , set that ViewController as the windows rootViewController (and also don't forget to create the window itself). @DánielNagy it works!


2 Answers

try this

Objective-C

UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; topWindow.rootViewController = [UIViewController new]; topWindow.windowLevel = UIWindowLevelAlert + 1;  UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"APNS" message:@"received Notification" preferredStyle:UIAlertControllerStyleAlert];  [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK",@"confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {     // continue your work      // important to hide the window after work completed.     // this also keeps a reference to the window until the action is invoked.     topWindow.hidden = YES; // if you want to hide the topwindow then use this     topWindow = nil; // if you want to remove the topwindow then use this  }]];  [topWindow makeKeyAndVisible]; [topWindow.rootViewController presentViewController:alert animated:YES completion:nil]; 

Swift3 and above

var topWindow: UIWindow? = UIWindow(frame: UIScreen.main.bounds) topWindow?.rootViewController = UIViewController() topWindow?.windowLevel = UIWindow.Level.alert + 1  let alert = UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in     // continue your work      // important to hide the window after work completed.     // this also keeps a reference to the window until the action is invoked.     topWindow?.isHidden = true // if you want to hide the topwindow then use this     topWindow = nil // if you want to hide the topwindow then use this  })  topWindow?.makeKeyAndVisible() topWindow?.rootViewController?.present(alert, animated: true, completion: nil) 

Detail description: http://www.thecave.com/2015/09/28/how-to-present-an-alert-view-using-uialertcontroller-when-you-dont-have-a-view-controller/

like image 178
Anbu.Karthik Avatar answered Sep 18 '22 09:09

Anbu.Karthik


Shortest & Simplest :

Create a extension :

extension UIApplication { class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {     if let navigationController = controller as? UINavigationController {         return topViewController(controller: navigationController.visibleViewController)     }     if let tabController = controller as? UITabBarController {         if let selected = tabController.selectedViewController {             return topViewController(controller: selected)         }     }     if let presented = controller?.presentedViewController {         return topViewController(controller: presented)     }     return controller } } 

and then use it anywhere like

UIApplication.topViewController()?.present(UIViewController, animated: true, completion: nil) 

With this you can present Alert or anything Anywhere Example :

let alert = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert) let cancelButton = UIAlertAction(title: "Ok", style: .cancel, handler: nil) alert.addAction(cancelButton) UIApplication.topViewController()?.present(alert, animated: true, completion: nil) 

ALTERNATE METHOD :

No need to create any Extension or any method or anything simply write the above 3 lines for creating an Alert and for presenting use :

self.window?.rootViewController?.present(alert, animated: true, completion: nil) 

That's it.! =)

like image 39
Yash Bedi Avatar answered Sep 22 '22 09:09

Yash Bedi