Abhinav Abhinav - 1 year ago 96
iOS Question

UIAlertController not showing from AppDelegate

I am trying to present an

globally (from a place which is not a view controller) like this but my alert does not show up. I am testing my initial alert which is shown by reachability test in App Delegate after detecting no network. I can see launch image on my screen but alert does not show up. If I use
, everything looks good.

If you had encountered similar issue then please advise.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:iTitle message:iMessage preferredStyle:UIAlertControllerStyleAlert];
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

if ([rootViewController isKindOfClass:[UINavigationController class]]) {
rootViewController = [((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];

dispatch_async(dispatch_get_main_queue(), ^{
[rootViewController presentViewController:alert animated:YES completion:^{
[aBlockSelf alertPresented];


Why am I showing it from

As I said, I am showing an alert to user as soon as I execute my reachability test. This test has to be performed from within
even before I load any view controller. So, I show a splash screen on my
and I am expecting my alert to come on top of it.

Is my window set up correctly when I present the alert?

Yes, I am ensuring that I present alert controller post window is correctly set and that after below lines are executed!

[self.window addSubview:self.rootViewController.view];
[self.window makeKeyAndVisible];

Answer Source

Alright, I solved this problem by creating a UIWindow object with an empty view controller and presenting my alert in it. Apparently, once alert controller is dismissed, this temporary window also go away.

self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertControl animated:YES completion:^{
    [aBlockSelf alertPresented];

Thanks to this SO thread where a WWDC lab session is referred :-)!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download