[ios] Applications are expected to have a root view controller at the end of application launch

I get the following error in my console:

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

Below is my application:didFinishLaunchWithOptions method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Set Background Color/Pattern
    self.window.backgroundColor = [UIColor blackColor];
    self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
    //self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"testbg.png"]];

    // Set StatusBar Color
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];

    // Add the tab bar controller's current view as a subview of the window
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

In Interface Builder, the UITabBarController's delegate is hooked up to the App Delegate.

Anyone know how to fix this issue?

This question is related to ios objective-c

The answer is


I had this same error message in the log. I had a UIAlertView pop up in application:didFinishLaunchingWithOptions. I solved it by delaying the call to the alertView to allow time for the root view controller to finishing loading.

In application:didFinishLaunchingWithOptions:

[self performSelector:@selector(callPopUp) withObject:nil afterDelay:1.0];

which calls after 1 second:

- (void)callPopUp
{
    // call UIAlertView
}

Although a lot of these answers seem valid, none of them fixed the message for me. I was experimenting with the empty application template and trying to load straight to a .xib file for understanding sake (like to old window template). It seems like Apple left an NSLog message running.

I was on Xcode 4.3 and nothing seemed to get rid of the message and I wanted to know why. Finally I decided to see what would happen in Xcode 4.5 (preview/iPhone 6.0 build) and the message is no longer there. Moving on.


I began having this same issue right after upgrading to Xcode 4.3, and only when starting a project from scratch (i.e. create empty project, then create a UIViewController, and then Create a separate nib file).

After putting ALL the lines I used to, and ensuring I had the correct connections, I kept getting that error, and the nib file I was trying to load through the view controller (which was set as the rootController) never showed in the simulator.

I created a single view template through Xcode and compared it to my code and FINALLY found the problem!

Xcode 4.3 appears to add by default the method -(void)loadView; to the view controller implementation section. After carefully reading the comments inside it, it became clear what the problem was. The comment indicated to override loadView method if creating a view programmatically (and I'm paraphrasing), otherwise NOT to override loadView if using a nib. There was nothing else inside this method, so in affect I was overriding the method (and doing nothing) WHILE using a nib file, which gave the error.

The SOLUTION was to either completely remove the loadView method from the implementation section, or to call the parent method by adding [super loadView].

Removing it would be best if using a NIB file as adding any other code will in effect override it.


Make sure that your "Is Initial View Controller" is correctly set for your first scene.enter image description here

That's what's causing the error.


There was a slight change around iOS 5.0 or so, requiring you to have a root view controller. If your code is based off older sample code, such as GLES2Sample, then no root view controller was created in those code samples.

To fix (that GLES2Sample, for instance), right in applicationDidFinishLaunching, I create a root view controller and attach my glView to it.

- (void) applicationDidFinishLaunching:(UIApplication *)application
{
  // To make the 'Application windows are expected
  // to have a root view controller
  // at the end of application launch' warning go away,
  // you should have a rootviewcontroller,
  // but this app doesn't have one at all.
  window.rootViewController = [[UIViewController alloc] init];  // MAKE ONE
  window.rootViewController.view = glView; // MUST SET THIS UP OTHERWISE
  // THE ROOTVIEWCONTROLLER SEEMS TO INTERCEPT TOUCH EVENTS
}

That makes the warning go away, and doesn't really affect your app otherwise.


OrdoDei gave a correct and valuable answer. I'm adding this answer only to give an example of a didFinishLaunchingWithOptions method that uses his answer as well as accounting for the others’ comments regarding Navigation Controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    // Instantiate the main menu view controller (UITableView with menu items).
    // Pass that view controller to the nav controller as the root of the nav stack.
    // This nav stack drives our *entire* app.
    UIViewController *viewController = [[XMMainMenuTableViewController alloc] init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

    // Instantiate the app's window. Then get the nav controller's view into that window, and onto the screen.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // [self.window addSubview:self.navigationController.view];
    // The disabled line above was replaced by line below. Fixed Apple's complaint in log: Application windows are expected to have a root view controller at the end of application launch
    [self.window setRootViewController:self.navigationController];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

i got this problems too. i got my project run in xcode4.2.1. i've read all comments up there, but no one is cool for me. after a while, i find that i commented a piece of code.

//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

then i uncommented it. everything is ok for me. hope this would helpful for you guys.


With my first view being MenuViewController I added:

MenuViewController *menuViewController = [[MenuViewController alloc]init];
self.window.rootViewController = menuViewController;

on the App Delegate method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}

That worked.


None of the above suggestions solved my problem. Mine was this:

Add:

window.rootViewController = navigationController;

after:

[window addSubview:navigationController.view];

in my appdelegate's

- (void)applicationDidFinishLaunching:(UIApplication *)application {

I upgraded to iOS9 and started getting this error out of nowhere. I was able to fix it but adding the below code to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
    if(window.rootViewController == nil){
        UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
        window.rootViewController = vc;
    }
}

Received the same error after replacing my UI with a Storyboard, using XCode 4.6.3 and iOS 6.1

Solved it by clearing out all of the code from didFinishLaucnhingWithOptions in the AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}

I had the same error when trying to change the first view controller that was loaded in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

At first I didn't really know where the error was coming from precisely so I narrowed it down and found out what went wrong. It turns out that I was trying to change the display of a view before it actually came on screen. The solution was hence to move this code in the viewcontroller that was giving me trouble from

- (void)viewDidLoad

to

- (void)viewDidAppear:(BOOL)animated

and the error stopped appearing. My problem specifically was caused by making a UIAlertView show.

In your case I suggest you check out the code in the tabBarController's active view controller (as it is probably a problem in that view controller). If that doesn't work, try to set the starting settings in the nib file instead of in code - or if you want to do it in code, try moving the code to the tabBarController's active viewcontroller's appropriate method.

Good luck!


I run into the same problem recently, when building a project with ios5 sdk. At first it was building and running properly, but after that the error appeared.
In my case the solution was rather simple.
What was missing, was that somehow the Main Interface property in the summary tab of my application target got erased. So I needed to set it again.


If this is not the point, and if the tabBarController is still nil, you can always programmatically create your window and root controller. As a fallback I added the following code to my project

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    if (!window && !navigationController) {
        NSLog(@"Window and navigation controller not loaded from nib. Will be created programatically.");
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        UIViewController *viewController1, *viewController2;
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];

        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
        self.window.rootViewController = self.tabBarController;

    }
    else {
        [window addSubview:[tabBarController view]];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

This will work only if sho's solution is implemented also.


I was able to set the initial view controller on the summary screen of xcode.

Click on the top most project name in the left hand file explorer (it should have a little blueprint icon). In the center column click on your project name under 'TARGETS', (it should have a little pencil 'A' icon next to it). Look under 'iPhone / iPod Deployment Info' and look for 'Main Interface'. You should be able to select an option from the drop down.


I had the same error message because I called an alert in

- (void)applicationDidBecomeActive:(UIApplication *)application 

instead of

- (void)applicationWillEnterForeground:(UIApplication *)application

for ARC change:

change to

@synthesize window;

instead of

@synthesize window=_window;

I was getting this error (Applications are expected to have a root view controller at the end of application launch), and I was creating the view controllers programmatically.

Solved it by ensuring the loadView method in my root view controller was calling [super loadView].


Validate your STORYBOARD file... If you are using IB to 'wire' code to your UI.. and just happen to delete the UI element ... and then realize, oh you need to delete the reference in the code (in my case a UIButton called AbortButton)... then moments later the app won't launch...

The issue is in the 'storyboard' file. Open it in an xml editor and look for orphaned 'relationships', of connectors which point places where they should not...

eg:

<relationships> <relationship kind="outlet" name="AbortButton" candidateClass="UIButton"/>

did NOT exist in my code anymore... (yet i still had a 'connector' linked up in the storyboard file)..

Also i had (somehow and invalid connector from a UIImageView or a ToolBar to my main UIViewController.view object)... don't think it liked that either...

bottom line - one doesn't have to manually 'code the controller' back into existence... the STORYBOARD file is whacked. I fixed mine, and bingo worked perfect again..

and BTW, as a text coder.. i am growing very fond of IB... kicks butt over MS Visual Studio's IDE...


I came across the same issue but I was using storyboard

Assigning my storyboard InitialViewController to my window's rootViewController.

In

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
UIStoryboard *stb = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [stb instantiateInitialViewController];
return YES;
}

and this solved the issue.


This occurred for me because i inadvertently commented out:

[self.window makeKeyAndVisible];

from

- (BOOL)application:(UIApplication*) didFinishLaunchingWithOptions:(NSDictionary*)

This issue happens when you don't have Interface Builder set up correctly.

Ensure your App Delegate's window and viewController outlets are hooked up:

In your MainWindow.xib, hold control, click App Delegate and drag to the Window object. Select window. Hold control and select the App delegate again, drag to your root view controller and select viewController.


  • Select your "Window" in your Nib File
  • In "Attributes Inspector" Check "Visible at Launch"

image![]

  • This happens when your nib file is created manually.
  • This fix works for regular nib mode - not storyboard mode

I also had this problem. Turns out it was when I deleted the App Delegate from the Objects list on the left I deleted the connection for App Delegate, Window and TabBarController :)


I ran into this in an iPad application targeting iOS 5.1 in Xcode 4.5.1. The app uses UITabBarController. I needed a new section within the tab bar controller, so I created a new view controller and xib. Once I added the new view controller to the tab bar controller, none of my on-screen controls worked anymore, and I got the "expected to have a root view controller" log.

Somehow the top-level object in the new xib was UIWindow instead of UIView. When I dropped a UIView into the XIB, had the view outlet point to it, moved all the subviews into the new UIView, and removed the UIWindow instance, the problem was fixed.


Sounds like self.tabBarController is returning nil. tabBarController probably is not wired up in Interface Builder. Set the IBOutlet of tabBarController to the tabBarController in Interface Builder.


I also had this error but unlike any of the answers previously listed mine was because i had uncommented the method 'loadView' in my newly generated controller (xcode 4.2, ios5).

 //Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView  
{
}

It even told me that the method was for creating the view programmatically but i missed it because it looked so similar to other methods like viewDidLoad that i normally use i didn't catch it.

To solve simply remove that method if you are not programmatically creating the view hierarchy aka using nib or storyboard.


None of the answer quite fixed my issue.

I am working on an old iOS4 project upgraded to ARC and now being worked on in Xcode 5 for iOS 7

I read through all of them and started checking my config and code.

What fixed it for me was adding

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
    // Maybe do something
    return YES;
}

In addition to having

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
}

I did not have

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions

prior to getting the error.


This error also show up when file's owner of MainWindow.xib is set incorrectly.

File's owner is UIApplication
->inserted object of app delegate class with window outlet connected to window


Make sure you have this function in your application delegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions {
   return YES;
}

Make sure didFinishLaunchingWithOptions returns YES. If you happened to remove the 'return YES' line, this will cause the error. This error may be especially common with storyboard users.


This error hit me all of a sudden. So, what caused it?

It turns out I was in IB attaching File Owner to a new small ImageView I'd dragged onto the View. I hadn't called it an IBOutlet in the .h file, so when I ctrl-dragged to it, the new Imageview wasn't listed as a possible connection. The only possibility displayed in the little black box was View. I must have clicked, inadvertently. I made a few changes then ran the program and got the Root Controller error. The fix was reconnecting File Owner to the bottom View in the xib - IB screen.


Have you tried setting the delegate, i.e.

self.rootController.delegate = self;

within applicationDidFinishLaunchingWithOptions? That worked for me, although I'm not sure why.


In my case, everything about the actual window and the didFinishLaunchingWithOptions: method was fine.

My error was that I didn't realize applicationDidBecomeActive: runs on startup in addition to when the app is coming to the foreground after having been in the background.

Therefore, in applicationDidBecomeActive: I was manipulating view controllers that hadn't finished all their setup yet (waiting for different threads to respond, etc).

Once I moved this functionality outside of applicationDidBecomeActive, the errors went away.


I got this when starting with the "Empty Application" template and then manually adding a XIB. I solved it by setting the main Nib name as suggested by Sunny. The missing step in this scenario is removing

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

from

application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

As it will overwrite the instance of your window created in the Xib file. This is assuming you have created a ViewController and wired it up with your window and App Delegate in the XIB file as well.


I had the same problem. If you're building a window-based application "from scratch" as I was, you'll need to do the following: (note, these are steps for Xcode 4.2.)

0. Make sure your application delegate conforms to the UIApplicationDelegate protocol.

For example, suppose our delegate is called MyAppDelegate. In MyAppDelegate.h, we should have something like this:

@interface MyAppDelegate : 
    NSObject <UIApplicationDelegate> // etc...

1. Specify the application delegate in main.m

For example,

#import "MyAppDelegate.h"

int main(int argc, char *argv[])
{
  @autoreleasepool {
    return UIApplicationMain(argc, argv,
      nil, NSStringFromClass([MyAppDelegate class]));
  }
}

2. Create a main window interface file.

To do this, right-click on your project and choose New File. From there, choose Window from the iOS -> User Interface section.

After adding the file to your project, go to the project's summary (left-click on the project; click summary.) Under iPhone/iPod Deployment Info (and the corresponding iPad section if you like) and select your new interface file in the "Main Interface" combo box.

3. Hook it all up in the interface editor

Select your interface file in the files list to bring up the interface editor.

Make sure the Utilities pane is open.

Add a new Object by dragging an Object from the Objects list in the Utilities pane to the space above of below your Window object. Select the object. Click on the Identity inspector in the Utilities pane. Change the Class to the application's delegate (MyAppDelegate, in this example.)

Bring up the connections inspector for MyAppDelegate. Connect the window outlet to the Window that already exists in the interface file.

Click on File's Owner on the left, and then click on the Identity inspector in the Utilities pane. Change the Class to UIApplication

Bring up the connections inspector for File's Owner. Connect the delegate outlet to the MyAppDelegate object.

4. Finally, and very importantly, click on the Window object in the interface file. Open the Attributes inspector. Make sure "Visible at Launch" is checked.

That's all I had to do to get it working for me. Good luck!


On top of "sho" answer, that is correct (fourth parameter of UIApplicationMain should be the name of the main controller), I add some comments.

I have recently changed the 'model' of an app of mine from using MainWindow.xib to construct a window programatically. The app used an older template that created that MainWindow automatically. Since I wanted to support a different controller view XIB for iPhone 5, it is easier to choose the right XIB programatically when the App Delegate is created. I removed MainWindow.xib from project as well.

Problem was, I forgot to fill the fourth parameter in UIApplication main and I FORGOT TO REMOVE MainWindow from "Main Interface" at Project Summary.

This caused a BIG problem: it rendered the harmless warning "Applications are expected to..." on development devices, but when it went to App Store, it broke on consumer phones, crashing because MainWindow was no longer in the bundle! I had to request an expedited review for the bugfix.

Another sympthom is that sometimes a white block, like a blank UIView, was sometimes appearing when Settings were changed and app was put in foreground. In iPhone 5 it was clear that it was an 320x480 block. Perhaps the missing MainWindow was being created in development mode, using the old size. I had just found this bug when the first reports of the crash reached the inbox.

Installing the app from App Store instead of from XCode showed that the app indeed crashed, and the MainWindow issue revealed itself on log, so I could see that it was not some special combination of devices+IOS versions.


Try to connect IBOutlet of tab bar controller to root view in the Interface Builder instead of

self.window.rootViewController = self.tabBarController;

But actually I haven't seen such error before.


Moving setRootViewController: from didFinishLaunchingWithOptions: to awakeFromNib: solved this in my empty project.


To add to Mike Flynn's answer, since upgrading to Xcode 7 and running my app on an iOS 9 device, I added this to my (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// Hide any window that isn't the main window
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *window in windows) {
    if (window != self.window) {
        window.hidden = YES;
    }
}

I had this error too but no answer already listed was solving my issue. In my case the log display was due to the fact I was assigning the application root view controller in another sub-thread.

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    ...
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        ...
        dispatch_async(dispatch_get_main_queue(), ^{
            ...
            [self updateTabBarTitles];
            self.window.rootViewController = self.tabBarController;
            ...
        });
    });

    [self.window makeKeyAndVisible];
    return YES;
}

By moving the rootViewController assignment to the end of the function - just before the call to makeKeyAndVisible: - causes the log message not to be displayed again.

{
    ...
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

I hope this helps.


This Swift 2 solution worked for me :

Insert the code below in AppDelegate -> didFinishLaunchingWithOptions

self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("YourRootViewController") as? YourRootViewControllerClass

If you use MTStatusBarOverlay, then you'll get this error.

MTStatusBarOverlay creates an additional window ([[UIApplication sharedApplication] windows) which doesn't have a root controller.

This doesn't seem to cause a problem.


Replace in AppDelegate

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

None of the above worked for me... found out something wrong with my init method on my appDelegate. If you are implementing the init method make sure you do it correctly

i had this:

- (id)init {
    if (!self) {
        self = [super init];
        sharedInstance = self;
    }
    return sharedInstance;
}

and changed it to this:

- (id)init {
    if (!self) {
        self = [super init];
    }
    sharedInstance = self;
    return sharedInstance;
}

where "sharedInstance" its a pointer to my appDelegate singleton


I was migrating an old EAGLView sample project to the new GLKView sample project in Xcode 4 and none of the solutions worked for me. Finally I realized that I was trying to set the main GLKViewController's self.view to point to a nested GLKView in Interface Builder.

When I pointed the self.view back to the root GLKView in Interface Builder, my app was then able to launch without an error message (so make sure your view controller's view is set to the root view).

P.S. if you want to get a nested GLKView working, create a new member variable like self.glkSubview in ViewController.h and drag its connection to the nested GLKView in Interface Builder. Then make sure to drag self.glkSubview's delegate to File's Owner. You must manually call [self.glkSubview display] in "- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect" if you have setNeedsDisplay turned off for the GLKView.


This happened to me. Solved by editing .plist file. Specify the Main nib file base name.(Should be MainWindow.xib). Hope this will help.

enter image description here


If your app replaces the main UIWindow with FingerTipWindow (to show touches on a projector) and you haven't updated your sources for a few (several) years, your replacement object might not include a rootViewController property (see kgn's 5/21/2013 mod at GitHub)

You can set window.rootViewController in didFinishLaunchingWithOptions until the cows come home, but your window will not report one "at end of application launch" and will throw an exception at runtime. Update your sources.


how to add a RootViewController for iOS5

if your app didn't use a RootViewController till now, just create one ;) by hitting File > New > New File; select UIViewController subclass name it RootViewController, uncheck the With XIB for user interface (assuming you already have one) and put this code in your AppDelegate :: didFinishLaunchingWithOptions

rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;

for sure - you have to import RootViewController.h and create the variable

here is a nice article about the RootViewController and the AppDelegate,


I solved the problem by doing the following (none of the other solutions above helped):

From the pulldown menu associated with "Main Interface" select another entry and then reselect "MainWindow" then rebuild.

enter image description here