29

I'm attempting to present a view controller from the app delegate with this code:

- (void)interstitialViewControllerRequestSucceeded:(UIViewController *)interstitialViewController
{
    [self.window.rootViewController presentViewController:(UIViewController *)interstitialViewController animated:YES completion:NULL];
}

It will display the interstitial on the initial view controller but none of the others. I want it to display on every one attached to a navigation controller.

How can I modify this code to achieve that goal?

Nazik
  • 8,607
  • 26
  • 75
  • 122
mikeholp
  • 945
  • 3
  • 11
  • 14

3 Answers3

52

You can also try:

[[[UIApplication sharedApplication] keyWindow] rootViewController]

How I use it:

#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
[ROOTVIEW presentViewController:interstitialViewController animated:YES completion:^{}];
WrightsCS
  • 50,205
  • 22
  • 134
  • 184
11

Swift 3 version for checked response :

UIApplication.shared.keyWindow?.rootViewController
Kevin ABRIOUX
  • 14,731
  • 9
  • 87
  • 84
8

Swift 5 and up

Check this:

UIApplication.shared.windows.first?.rootViewController
Jonas Deichelmann
  • 3,063
  • 1
  • 28
  • 38