92

After running function

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

there is a crash:

 Assertion failure in 
-[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', `enter code here`reason: 'Application windows are expected to have a root view controller at the end of application launch'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000109377885 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108df0df1 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001093776ea +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x0000000108a42bb1 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   UIKit                               0x000000010760e350 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2875
    5   UIKit                               0x000000010760b73f -[UIApplication workspaceDidEndTransaction:] + 188
    6   FrontBoardServices                  0x000000010b87fd7b FrontBoardServices + 163195
    7   FrontBoardServices                  0x000000010b880118 FrontBoardServices + 164120
    8   CoreFoundation                      0x00000001092a20f1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    9   CoreFoundation                      0x0000000109297eac __CFRunLoopDoSources0 + 556
    10  CoreFoundation                      0x0000000109297363 __CFRunLoopRun + 867
    11  CoreFoundation                      0x0000000109296d78 CFRunLoopRunSpecific + 488
    12  UIKit                               0x000000010760b091 -[UIApplication _run] + 402
    13  UIKit                               0x000000010760f79b UIApplicationMain + 171
    14  bbwc                                0x00000001037a9998 main + 344
    15  libdyld.dylib                       0x000000010a45ca05 libdyld.dylib + 10757
    16  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

This project is an old project, what should I do to make it build and run with Xcode 7 and iOS 9?

3

10 Answers 10

178

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];
13
  • 1
    I have the same problem and I think it's an iOS 9 issue. My project works in iOS 7 & 8. For some reason the view from the storyboard is not set correctly. Jun 18, 2015 at 13:16
  • 3
    Thank you very much . the answer is in the error message:"Application windows"I find there is two window in my project ,one is normal widdow,the other is a third party modual called Jun 19, 2015 at 3:57
  • 3
    MTStatusBarOverlay and it does not have a RootViewController,iOS9 require all the Window must have a rootViewController. Jun 19, 2015 at 4:07
  • 1
    Yes, I found the same issue, eventually. My app has an additional window which does not have a root view controller. Jun 19, 2015 at 8:24
  • 1
    I've found the solution who works for me: stackoverflow.com/a/32719949/1881895
    – barrast
    Nov 23, 2015 at 14:32
37

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.

6
  • 2
    Thanks!! This turned out to be the issue for me. I had set the main view controller, but the app had a second window which I was unaware of.
    – n13
    Sep 23, 2015 at 0:53
  • So solid...I'm no rookie and this one got me when updating to Xcode 7.1 running iOS 9.1...status bar has a window dedicated...of course it does?! I'm hiding the status bar if that means anything to anyone.
    – whyoz
    Nov 5, 2015 at 5:44
  • Wow I had 2 out of 12 of my apps crashing for App review and this seems to fix it. Dec 1, 2015 at 22:25
  • Worked great!! Gotta love "Apparently there is a window dedicated to the status bar which typically causes this issue. " Did NOT see that coming.
    – eGanges
    Feb 24, 2016 at 7:01
  • 1
    This method may result in viewDidLoad and viewWillAppear being called twice on your rootViewController. If you have a manually init window, check your plist and make sure you DON'T have the Window.xib defiined as the "Main nib file base name" if you're seeing things called twice after using this workaround. Then you just need remove this code and setRootViewController like you would normally.
    – whyoz
    May 5, 2016 at 13:13
21

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)!

2
  • Welcome to Stack Overflow! Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. Sep 17, 2015 at 15:12
  • Thanks, that avoided my iOS ~3–8 warning => iOS 9 crash, but gave a static analyzer warning about a leak. So I moved the declaration to the interface in the header, with the assignment in applicationDidFinishLaunching. I then added [vc release] to dealloc. Feb 1, 2016 at 18:53
13

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.

1
  • You are brilliant :D, thanks man i just comment the initialisation of window and all is OK now Nov 30, 2015 at 23:35
3

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.

2

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

1
  • Your answer helped me
    – Aznix
    Oct 7, 2015 at 13:47
2

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 Set your Main Interface to Main.storyboard
1

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
0
1

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.

0
0

Swift 2 solution that worked for me :

Insert the code below in AppDelegate -> didFinishLaunchingWithOptions

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.