iOS: shouldAutorotateToInterfaceOrientation / Lock Orientation in PhoneGap

Starting in PhoneGap 0.9.5 there’s an undocumented iOS only feature that allows you to handle whether the device should rotate to a particular orientation. It exposes the iOS Objective-C method shouldAutorotateToInterfaceOrientation with a JavaScript function shouldRotateToOrientation.

function shouldRotateToOrientation (rotation) {
    switch (rotation) {
        //Portrait or PortraitUpsideDown
        case 0:
        case 180:
            return true;
        //LandscapeRight or LandscapeLeft
        case 90:
        case -90:
             return false;
    }
}

This needs to be in the global scope of your JavaScript and will get called before any rotations occur, so keep it lean to avoid locking up the UI. Returning true allows the device to rotate, returning false prevents it.

Works great in your HTML5 iOS, iPhone and iPad apps, sadly currently there’s no Android or other platform integration. Hopefully we’ll see a cross-platform method soon!

7 thoughts on “iOS: shouldAutorotateToInterfaceOrientation / Lock Orientation in PhoneGap

  1. Hi,
    Is there any way locking the orientation in Iphone without using phonegap…?

  2. Thanks for posting this. I found the code in the .m file and needed some verification on how to use it from javascript. If you hadn’t posted this, I’d still be looking.

  3. Now what would be great is the ability to set the orientation of the view.
    In my case I want the picture lightbox to be able to rotate and the rest to be in portrait mode…
    but when i come back from a landscaped lightbox the app stays in landscape format :/

  4. Having the same problem as Alexander. Anybody found a solution? Thanks!!

  5. Thank you!
    I can confirm this still works on PhoneGap 3.0/Cordova 3.0 on iOS 6.1.3 using an iPad.

Comments are closed.