Error "Application windows are expected to have a root view controller" (iOS)

Kit Ng picture Kit Ng · Oct 6, 2013 · Viewed 12.3k times · Source

I've created a blank iPhone app project and would like to show a full-screen advertisement during app launch.

I tried to install the ad by following this guideline: https://github.com/mopub/mopub-ios-sdk/wiki/Interstitial-Integration-For-iOS

That's what I've done finally:

In ViewController.h

In ViewController.m

Actually all codes are just copied from the previous link.

However, an error shows when app runs:

Application windows are expected to have a root view controller at the end of application launch

I think this error may probably related to the loadView method, because if I remove the loadView method, the error disappeared.

In fact, this error seems common as it can be easily searched on the internet, but I don't know how loadView is related to it, and how can it be solved in my case.

Any solutions? Thanks a lot.

Answer

Matt Tang picture Matt Tang · Oct 6, 2013

You probably need to do this:

Add

#import "ViewController.h" 

to the top of AppDelegate.m

And in AppDelegate.m, your application:didFinishLaunchingWithOptions: method should have some code like this.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ... Other code

    // Override point for customization after application launch.
    ViewController *viewController = [[ViewController alloc] init];

    self.window.rootViewController = viewController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}