-1

How can I use UIAlertViewController in appdelegate class.

Getting error no visibility @interface for appdelegate declare the selector

Serjik
  • 10,543
  • 8
  • 61
  • 70
  • u can't present alert box...!!! – Yagnesh Dobariya Feb 16 '16 at 04:24
  • just `[alert show];` – KDeogharkar Feb 16 '16 at 04:25
  • @KDeogharkar its UIAlertView, this is UIAlertController and it needs another controller to show it, but rather use UIAlertView for simple showing – Tj3n Feb 16 '16 at 04:28
  • I got your point but in future UIAlertView may not work for ios 9. – S.Dharmwan Feb 16 '16 at 04:31
  • oh sorry @Tj3n . i missed that my mistake. – KDeogharkar Feb 16 '16 at 04:32
  • UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [vc presentViewController:showForegroundNotificationAlertController animated:YES completion:nil]; Correct working this one...... Thanks. – S.Dharmwan Feb 26 '16 at 04:42
  • UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [vc presentViewController:showForegroundNotificationAlertController animated:YES completion:nil]; Correct working this one...... Thanks. – S.Dharmwan Feb 26 '16 at 04:44

1 Answers1

1

if you want to show in UIAlertviewController in Appdelegate, you can do it two ways

Choice-1

Initially you get the visible/Top ViewController from window , assume that you were in some where on any viewcontrollerm at that time you access that from root view controller , use this

    UIViewController *vc = [self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    //second add your present on`UIAlertController`
   [vc presentViewController:alert animated:YES Completion:nil];

For get which viewcontroller at visible for Reference purpose I take the answer From here

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil)
    {
        return rootViewController;
    }
    if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
    
        return [self visibleViewController:lastViewController];
    }
    if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]])
    {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
        UIViewController *selectedViewController = tabBarController.selectedViewController;
    
        return [self visibleViewController:selectedViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;

    return [self visibleViewController:presentedViewController];
}

Swift

 let navigationController = application.windows[0].rootViewController as UINavigationController

  let vc = navigationController.visibleViewController
   vc.presentViewController(alert, animated: true, completion: nil)

Choice-2

If you want to present the UIAlertController in directly in Appdelegetae , do like simple

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

Update

[ [[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES Completion:nil];
Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Still getting same error.... [self.window.rootViewController presentViewController:alert animated:YES Completion:nil]; – S.Dharmwan Feb 16 '16 at 10:09
  • Thanks for your help bro. but still getting same error.... I m confuse about what should I do. UIAlertController *alert= [UIAlertController alertControllerWithTitle:Title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ }]; [alert addAction:OK]; [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES Completion:nil]; – S.Dharmwan Feb 17 '16 at 05:04
  • @S.Dharmwan -- there is no more choice bro, else do like create a NSObject Class on that class add your UIAlert Controller , then call where you need , it works... – Anbu.Karthik Feb 17 '16 at 05:10