Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On iOS 6, PhoneGap 2.1 iPad app no longer autorotates

When running the app on iOS 6, my app no longer successfully autorotates. I have updated to Cordova 2.1, and I have the following code in my MainViewController.m file (which is a subclass of CDViewController, to be compatible with the new iOS6 way of handling autorotation:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

// iOS 6
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger ret = 0;

    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait])
        ret = ret | (1 << UIInterfaceOrientationPortrait);
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortraitUpsideDown])
        ret = ret | (1 << UIInterfaceOrientationPortraitUpsideDown);
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight])
        ret = ret | (1 << UIInterfaceOrientationLandscapeRight);
    if ([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft])
        ret = ret | (1 << UIInterfaceOrientationLandscapeLeft);

    return ret;
}
like image 885
jungziege Avatar asked Dec 11 '22 21:12

jungziege


2 Answers

In your AppDelegate.m you need to add the following to didFinishLaunchingWithOptions

[self.window setRootViewController:self.viewController];

Once you add this, rotation should start working again. It has for my two apps.

like image 141
mattdryden Avatar answered Dec 28 '22 07:12

mattdryden


Still too new here to get the ability to vote up mattdryden's answer in this thread. However, I wanted to second his answer and, to add value, note that his suggested fix also works for PhoneGap/Cordova 1.9

I have a few apps that haven't been through the PhoneGap/Cordova 1.9 --> 2.0/2.1 update process yet and manually making the change suggested above in AppDelegate.m worked for those apps.

Also, it's worth adding that where you put that line seems to matter.

I initially added this line just before: return YES and it failed. It turns out that you need to place it before this line:

[self.window addSubview:self.viewController.view];

One more thing.... to help Google/etc. find this problem faster... a key hint in the console log that you need to add this line is the following:

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

Adding the line of code mentioned above makes this error go away...

Hope this helps others seeing this problem.

like image 20
JA_251 Avatar answered Dec 28 '22 07:12

JA_251