Launch orientation in iOS 4

Written by David Frampton @ 8:46 am, July 8, 2010

This is just a very quick post, showing code I found I needed to deal with the way my app was launched on iOS4. In a nutshell I was finding that iOS4 would launch my app upside down (UIInterfaceOrientationLandscapeLeft) when the iPhone was being held in the¬†UIInterfaceOrientationLandscapeRight orientation. I then wasn’t ever getting notified that the orientation was actually¬†UIInterfaceOrientationLandscapeRight. I’m not sure if this is a common problem, or if it is unique to something strange I am doing, but I am seeing correct behavior on all 3.x iOSs prior to 4.0.

So the code checks for 4.x, checks for an unknown or flat device rotation and then sets the status bar orientation to UIInterfaceOrientationLandscapeRight. Even if the device is actually being held UIInterfaceOrientationLandscapeLeft, the result is fine, as I am then notified of this, and can rotate to landscape left. I call this code in my main UIWindow subclass method, but I imagine doing so in the app delegate should be fine.

I expect this is a bug in iOS 4.0, but the whole orientation system is such a mess I’m not sure what the desired behavior is supposed to be. So it may or may not change in 4.1, but this code shouldn’t cause any particularly nasty behavior if it does.

BOOL isAbove3_2 = NO;
NSString *reqSysVer = @"3.2";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
    isAbove3_2 = YES;
}

if(isAbove3_2)
{
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    if(deviceOrientation == UIDeviceOrientationFaceUp || deviceOrientation == UIDeviceOrientationUnknown)
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    }
}








7 Comments

  1. Dzamir

    Try to use [self interfaceOrientation] from a UIViewController, this will return the current orientation used by the interface, not the device orientation. I think that the problem is that the device orientation can be flat or unknown, but the interface orientation is always determined.

    Comment by Dzamir — July 8, 2010 @ 9:13 am


  2. David Frampton

    I tried that, and the statusBarOrientation, and the deviceOrientation. All of them were wrong in one way or another. They mostly gave UIInterfaceOrientationLandscapeRight when the device was actually being held UIInterfaceOrientationLandscapeLeft.

    Comment by David Frampton — July 8, 2010 @ 9:16 am


  3. David Frampton

    Sorry, I should have said thank you. I do appreciate the offer of a possible solution, even if I had already tried it.

    Comment by David Frampton — July 8, 2010 @ 9:42 am


  4. Dzamir

    No problem :-)

    Comment by Dzamir — July 8, 2010 @ 9:51 am


  5. baby-rabbit

    Did you specify all valid initial orientations in your plist? The UIViewController orientation *should* just work.

    Comment by baby-rabbit — July 8, 2010 @ 10:10 am


  6. David Frampton

    Yep, I have the array with two values, landscape left, and landscape right.

    Comment by David Frampton — July 8, 2010 @ 9:27 pm


  7. Damien

    Dzamir, you saved me! I was stuck on an issue when my application to get the orientation (it was unknown), and the [self interfaceOrientation] was THE thing for me.

    Comment by Damien — September 30, 2010 @ 8:46 pm


RSS feed for comments on this post.

Sorry, the comment form is closed at this time.