Hopefully to help someone else

When I was working on handling changing iPhone orientations, I ran into one major issue which doesn’t appear to have been addressed anywhere on the Internet.  So I figured that I’d post the solution I eventually found here, in the hopes that someone who has this problem in the future will find this blog post and the solution to their troubles.

Any non-programmers reading this blog may safely ignore this post.

When your application is running on an iOS device, you’re told the orientation in which the device is currently being held, and when that orientation changes.   In most iOS apps, you’ll provide a function “shouldAutoRotateToInterfaceOrientation” on your view controller, which will tell the iOS system whether or not you’ve accepted the new orientation.  (For example, many games will reject “Portrait”-type orientations, and all apps will reject “FaceUp” or “FaceDown” orientations)

However, programs which render using OpenGL (particularly games) usually don’t want to do that;  while it’s simple and convenient, it’s also extremely slow for various undocumented reasons.  Instead, when using OpenGL, you probably want to just change your OpenGL settings to adapt your drawing to the new device orientation.  There are plenty of tutorials about how to do that, online.

What they didn’t mention is this:

When you double-tap the home button on the iPad (with firmware 4.2 installed.  Not sure if that’s been released publicly yet?), it lifts the screen up to show the fast-app-switcher bar.  New on the iPad is that it lifts the screen upward, regardless of the orientation (whereas on the iPhone, it always ‘lifts’ away from the home button).  This leads to a small issue for iPad games which render using OpenGL:  since we’re not using “shouldAutoRotateToInterfaceOrientation” to tell iOS when we’ve accepted a new orientation, it doesn’t know which way it should “lift” the screen to reveal the fast-app-switcher.  So what iOS does is flip back to the initial orientation that the device was in when the game was launched for as long as the fast-app-switcher is open, and then returns.

So for example, if you were holding the device in Portrait mode when you launched the program, and then put it into Landscape after the game started, and then you double-tapped the home button, the game would switch back into Portrait mode until you closed the fast-app-switcher again.  And it does this because iOS has no way of knowing whether or not your app actually went into landscape mode when it asked you to.

So here’s how to fix it:

If you’re handling device orientation by modifying your rendering yourself (instead of having iOS do it for you automatically), you need to inform iOS when you accept a device orientation change.  You do this by calling:

UIApplication *theApp = [UIApplication sharedApplication];
theApp.statusBarOrientation = UIDeviceOrientationPortrait;

(Or whatever orientation you’re accepting).  Yes, you tell iOS that you’ve accepted a request for a different orientation by telling it where to put the status bar.  You need to do this even if you’re hiding the status bar.

Hope this helps somebody else avoid the lengthy struggles that I went through, trying to figure out how to make the fast-app-switcher work properly on iPad under 4.2!