Joey J
Joey J

Reputation: 1365

Why is showing a UIAlertView in application:didFinishLaunchingWithOptions: causing an error?

On the first run of my application I show an alert view to the user to choose iCloud or local document storage. Showing the alert view causes the following error:

Applications are expected to have a root view controller at the end of application launch wait_fences: failed to receive reply: 10004003

Why is this happening? How do you show an alert view on start-up without getting this error?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Check the user preferences for document storage options
    if (![UserPreferencesHelper userDocumentStoragePreferencesHaveBeenCreated])
    {
        // User preferences have not been set, prompt the user to choose either iCloud or Local data storage
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Use iCloud?" 
                                                        message:@"Would you like to store data in iCloud?" 
                                                       delegate:self 
                                              cancelButtonTitle:nil 
                                              otherButtonTitles:@"No", @"Yes", nil];
        [alert show];
    }
}

** UPDATE **

I should mention that I'm using iOS 5 with storyboards. The root view controller is set in the storyboard.

Upvotes: 2

Views: 1640

Answers (3)

Darren
Darren

Reputation: 25619

Try replacing [alert show] with:

[alert performSelector:@selector(show) withObject:nil afterDelay:0.0];

This delays the alert for a single pass through the runloop, presumably allowing your app's controllers and storyboards to complete their setup before the alert is presented.

Upvotes: 7

AtkinsonCM
AtkinsonCM

Reputation: 376

Before your app gets to the end of didFinishLaunchingWithOptions: it needs to have a rootViewController set. You can set this property for a ViewController named viewController with:

    self.window.rootViewController = self.viewController;

Note that setting a controller as the rootViewController automatically adds the view. So, you don't need to add the view again with:

[self setViewController:];

Upvotes: 0

smparkes
smparkes

Reputation: 14083

Like it says, you need a root controller for your app. Alerts appear above the normal controller-managed views so you need a controller-managed view for it to appear above.

Upvotes: 2

Related Questions