“Application windows are expected to have a root view controller at the end of application launch” error when running a project with Xcode 7, iOS 9

心已入冬 提交于 2019-11-26 12:56:57

From your error message:

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

How old is this "old" project? If it's more than a few years, do you still have:

[window addSubview:viewController.view];

You should instead replace it with:

[window setRootViewController:viewController];

If you have already set the rootViewController of your self.window in you app delegate and still getting this error at runtime, then you probably have more than one window in your UIApplication one of which may not have a rootViewController associated. You can loop through your app windows and associate an empty viewController to its rootViewController to fix the error you are getting.

Here's a code that loops through the app windows and associates an empty ViewController to the rootViewController if a window is missing it.

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

Update: Apparently there is a window dedicated to the status bar which typically causes this issue. The above code should fix this error.

XCODE 7 requires that all the Windows must have a rootViewController You can use easy:

UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
self.window.rootViewController = vc;

It's working good if you need to use only UIWindow (for easy examples from any Tutorials - before Xcode 7)!

It seems that since iOS 9.1(?) or Xcode 7.1 any UIWindow instantiated during application(_:didFinishLaunchingWithOptions:) needs to have a rootViewController set before leaving that method.

Previously it was sufficient for only the main window to have a rootViewController set during that method. Now any UIWindow instance needs to have a valid rootViewController property.

The culprit here could be your own code if you make use of UIWindow and also any other third party library that tries to initialize a new UIWindow instance during this time (like status bar message overlays, etc.).

NOTE: You also get the same error if you don't set the rootViewControleron your main window or if your storyboard is not set up right. Mentioning this as a side note since those cases are pretty obvious and straightforward to fix.

This has bitten me today too, and it cost me a few hours to fix it: my App has the window in a "MainWindow.xib", complete with navigation controller and accompanying root view controller, that were all automatically instantiated in the proper order, with Xcode 6 and iOS8.

On iOS9 that App still runs fine when downloaded from the AppStore, but not when newly built with Xcode 7 and run on iOS 9. At the time the app delegate is executing its applicationDidBecomeActive: method the root view controller is now not loaded, as it used to be before! That made the root view controller miss the call to my restore state code.

I fixed this by instantiating the root view controller myself, in code, and restoring its state from the viewDidLoad, explicitly.

You should set every window's rootviewcontroller property in your app

Just set your rootViewController to navigationController which is your UIViewController in the app-delegate.rb like my code below. I am new in ruby but hope this helped...

rootViewController = UIViewController.alloc.init

@window.rootViewController = navigationController

I came into this issue with an app I more ore less inherited. After verifying that the storyboard was properly set up as the apps main interface and that the storyboard had a RootViewController I was still getting the crash.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'

What I discovered after some further investigation that the crash was being caused by some view logic (SVProgressHud) being called in - (void)applicationDidBecomeActive:(UIApplication *)application. This seems to be new behavior in Xcode7 but as far as I can tell SVProgressHud was referencing the rootviewcontroller before it was set by the storyboard. Ultimately updating SVProgressHud to 2.0 fixed the bug.

I have an older project that worked in iOS 8 but not iOS 9. If your Main Interface is set to MainWindow.xib, update it to a storyboard. This fixed it for me:

  1. Create a new project, Single View Application is fine.
  2. Copy the Main.storyboard file to your project, or you could just create your own.
  3. Open your Project Settings and Set your Main Interface to Main.storyboard

Swift 2 solution that worked for me :

Insert the code below in AppDelegate -> didFinishLaunchingWithOptions

self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("YourRootViewController") as? YourRootViewControllerClass
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!