Lock orientation portrait/landscape iOS Phonegap

May 23, 2013

So in PhoneGap I had an issue that I’m sure many of you guys have had when working with a client. I had the normal PhoneGap app working in XCode and part of the functionality involves clicking a link, taking you to a ChildBroswer to view the link. The client wanted the app to be locked completely to portrait even when it is in the ChildBroswer. Luckily it’s not too hard but we do need to mess with some of the XCode project code.

I found this answer on StackOverflow FYI, thanks goes to mayuur.

In AppDelegate.m add this to the bottom.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     return (UIInterfaceOrientationMaskAll);
}

Then in the MainViewController.m you can add this if you want only Portrait:

- (BOOL)shouldAutorotate
{
     return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
     return (UIInterfaceOrientationMaskPortrait);
}

For Landscape do this instead:

- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskAllButUpsideDown);
    //OR return (UIInterfaceOrientationMaskAll);
}

Now, if you want to do some changes when Orientation changes, then use this function.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}