1

Every now and then, my app launches with a black screen, and the following warning:

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

I use storyboards. This tends to happen randomly from time to time when I compile the app, and the error tend only to disappear if I do changes to the code and/or storyboard and then recompile. A bad compilation will continue to launch the app in a bad state even if you restart it.

The only thing unusual (to me) about the app itself is that it's initial view contains a map view, with multiple container views (hidden at launch) on top of it.

I have tried using Spark Inspector to debug the view hierarchy when this happens, and it shows that the only element is the UIWindow, no UIViews are loaded at all.

This is my didFinishLaunchingWithOptions (which is the only method in the app delegate that has code in it), like Matt requested:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [DDLog addLogger:[DDASLLogger sharedInstance]];
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    UIColor *pink = [UIColor colorWithRed:(118 / 255.0)
                                    green:(214 / 255.0)
                                     blue:(84 / 255.0)
                                    alpha:1.0];
    [[DDTTYLogger sharedInstance] setForegroundColor:pink
                                     backgroundColor:nil
                                             forFlag:LOG_FLAG_INFO];

    #ifdef DEBUG
        DDLogInfo(@"Launching in debug mode");
    #else
        DDLogWarn(@"Launching in release mode");
    #endif

    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    [[AFNetworkActivityLogger sharedLogger] startLogging];

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
        setFont:[UIFont fontWithName:@"OpenSans" size:14]];

    NSMutableDictionary *titleBarAttributes = [NSMutableDictionary
        dictionaryWithDictionary:
            [[UINavigationBar appearance] titleTextAttributes]];
    [titleBarAttributes setValue:[UIFont fontWithName:@"OpenSans" size:18]
                          forKey:NSFontAttributeName];
    [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

    return YES;
}
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
  • What does your App Delegate code look like? – matt Sep 04 '14 at 03:54
  • Eliminate as much of that as you can and test launching some more. I'm thinking you are doing too much at launch and it's gumming up the works somehow. Also log your window's root view controller. It is supposed to have one and it seems like it isn't getting one or it's being set to nil somehow. – matt Sep 04 '14 at 04:14
  • Thanks, but I rebased everything in there to other parts of the code and I still get the error. – Daniel Larsson Sep 04 '14 at 05:39
  • Well, I don't know what else to tell you to try. It is clear that sometimes your window's `rootViewController` is not getting assigned, or is being nilified before launch. But this assignment is not some probabilistic matter; it's something that `UIApplicationMain` will absolutely do. You are not giving any clues to what the problem might be, except your mysterious phrase "A bad compilation will continue to launch the app in a bad state even afterwards." It shouldn't! A compile error should stop the build-and-run process dead. In General prefs, uncheck "Continue building after errors." – matt Sep 04 '14 at 15:44
  • I just sent out the app to a bunch of beta testers, every single one got a black screen at launch. I cleaned the project and archived and sent out another version, which all testers confirmed worked fine. Sorry about not being able to give more clues to what it might be, I simply don't have any. – Daniel Larsson Sep 09 '14 at 00:12
  • Cleaning the project often gets rid of cruft, like a previous version of a storyboard or xib, that you didn't realize was still there. I guess the question is whether the problem will subsequently return. If not, that was the answer all along. – matt Sep 09 '14 at 01:19
  • A clean will however not always fix the problem. Editing the storyboard on the other hand, will. When I get time, I will try to record my screen while this all is happening and when I fix it, I understand that you have your doubts. – Daniel Larsson Sep 09 '14 at 01:38
  • Could the storyboard be corrupted in some way? – matt Sep 09 '14 at 14:07
  • Actually, I just found out that the constraints in the storyboard are somewhat corrupted. This seems more like an xcode bug though: to fill the width (or height) of a superview that is 320px wide, you will need to have a subview that is 321px wide. Having that extra pixel aligns it perfectly. I have tried reinstalling xcode. Running xcode5 on Yosemite beta (with xcode6 beta also installed). – Daniel Larsson Sep 09 '14 at 16:23
  • Well, you've made it more interesting with your bounty. But you're going to lose the bounty rep without getting a satisfactory answer if you don't provide more information. No one can reproduce the problem (I've never seen anything like it). You will need to post your actual project, or at least a reduced version that demonstrates the issue. – matt Sep 09 '14 at 16:49
  • for testing - try to remove the "auto layout" flag from your storyboard - does the problem still persists? also it can be worth a try to check if all the files are included in your build target bundle. – shaish Sep 10 '14 at 00:34
  • did you find this post? (might be useful) http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati – longi Sep 10 '14 at 11:16

2 Answers2

1

If you are using storyboards you can set root view controller from storyboard enter image description here

Otherwise you must create it programmatically in AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *rootViewController = [[UINavigationController alloc] initWithNibName:@"RootVC" bundle:nil];
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    return YES;
}
Popeye
  • 11,839
  • 9
  • 58
  • 91
Narek Safaryan
  • 140
  • 1
  • 7
0

Nothing in your application:didFinishLaunchingWithOptions looks like it would screw up the root view controller for the window. Is the storyboard set in the Info.plist for the UIMainStoryboardFile key? In the storyboard is the view controller that is supposed to be the root view controller set as it in the inspector, there should be a check in the "Is Initial View Controller" checkbox. You should see a grey arrow pointing at the root view controller in the storyboard.

marchinram
  • 5,698
  • 5
  • 47
  • 59
  • Yes on all of your questions. Like I said, this error only occurs every now and then, usually it will runt just fine. – Daniel Larsson Sep 09 '14 at 01:27
  • I agree with this answer. The code you posted has no effect on your window or it's `rootViewController`. Check your Info.plist and main.m to see how your window and controller are created. – Rivera Sep 11 '14 at 00:54