Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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

Tags:

ios9

xcode7

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?

like image 729
andrew wang Avatar asked Jun 17 '15 07:06

andrew wang


4 Answers

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];
like image 63
James Webster Avatar answered Nov 17 '22 04:11

James Webster


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.

like image 29
Bms270 Avatar answered Nov 17 '22 04:11

Bms270


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

like image 21
Envoy Avatar answered Nov 17 '22 06:11

Envoy


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.

like image 13
lipka Avatar answered Nov 17 '22 05:11

lipka