Present UIAlertController from AppDelegate [duplicate] Present UIAlertController from AppDelegate [duplicate] ios ios

Present UIAlertController from AppDelegate [duplicate]


You can use this code as well if you want to launch it from didFinishLaunchingWithOptions.Hope this helps.

dispatch_async(dispatch_get_main_queue(), {              let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet)            self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil)        })

And up-to-date:

DispatchQueue.main.async {    let alert = UIAlertController(title: "Hello!", message: "Greetings from AppDelegate.", preferredStyle: .alert)    self.window?.rootViewController?.present(alert, animated: true, completion: nil)}


try this code..

-(void)application:(UIApplication *)application               didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"rakshapettu"); UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertView" message:@"I am an AlertView" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault                                                      handler:^(UIAlertAction * action) {                                                          [alert dismissViewControllerAnimated:YES completion:nil];                                                      }];[alert addAction:defaultAction];UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];alertWindow.rootViewController = [[UIViewController alloc] init];alertWindow.windowLevel = UIWindowLevelAlert + 1;[alertWindow makeKeyAndVisible];[alertWindow.rootViewController presentViewController:alert animated:YES completion:nil]; }


It's better to use something like:

var hostVC = self.window?.rootViewControllerwhile let next = hostVC?.presentedViewController {    hostVC = next}hostVC?.presentViewController(alertController, animated: true, completion: nil)

Previous answers won't work if you are already presenting modal view controller.