I m trying to display a UIAlertView from the appDelegate in didReceiveRemoteNotification when the app receive a push notification.
I ve this error :
Warning: Attempt to present <UIAlertController: 0x14c5494c0> on <UINavigationController:
0x14c60ce00> whose view is not in the window hierarchy!
here is my code :
func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary) {
var contentPush: NSDictionary = userInfo.objectForKey("aps") as NSDictionary
var message = contentPush.objectForKey("alert") as String
let alertController = UIAlertController(title: "Default Style", message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
let photoPushedVc = self.storyboard.instantiateViewControllerWithIdentifier("CommentTableViewController") as CommentTableViewController
println("the fetched post is \(post)")
photoPushedVc.post = post
let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController
activeVc?.presentViewController(photoPushedVc, animated: true, completion: nil)
}
alertController.addAction(OKAction)
let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController
activeVc?.presentViewController(alertController, animated: true, completion: nil)}
@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! Thank you!
Adding Action Buttons to Alert Dialog To create an action button that the user can tap on, we will need to create a new instance of an UIAlertAction class and add it to our alert object. To add one more button to UIAlertController simply create a new UIAlertAction object and add it to the alert.
To generate AlertController Dialog Box from AppDelegate using Objective-C,
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Hello World!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
Type 1
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
[alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
Type 2
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
[topController presentViewController:alertController animated:YES completion:nil];
Both are tested and working fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With