iPad device orientation always wrong when flat
Asked Answered
O

1

8

I know this subject gets a lot of attention here at SO, but none of the "solutions" is working for me. Here's the situation:

  1. iPad is flat on my desk (a face up device orientation), the home screen is perfectly readable, but the home button is on top. So this is effectively shows a "Portrait Up Side Down" interface orientation.

  2. I start my app

  3. The app calls beginGeneratingDeviceOrientationNotifications to make sure the accelerometer is working, and the orientation notifications come in.

  4. At startup, my app queries both the "interface orientation" and the "device orientation". The results are:

     STARTUP ORIENTATION:
         INTERFACE ORIENTATION: UIInterfaceOrientationPortrait
         DEVICE ORIENTATION: UIDeviceOrientationFaceUp
    

    The Apple docs say that each app (interface?) starts in portrait orientation, so this could make sense. The device orientation is also correct, because this is indeed face up.

  5. Right after launching, the first orientation notification is coming in:

    NOTIFICATION 1: 
        INTERFACE: UIInterfaceOrientationPortrait   
        DEVICE: UIDeviceOrientationPortrait
    

    Again, portrait for the interface, (which is contrary to what I'd expect, the home screen showed itself in up side down orientation), the device is said to be in portrait too (which isn't so, but this is apparantly a clever scheme to work out how to orient your app's contents when the device is face up or down).

  6. As many people mention, another (just one, not two) orientation notification follows:

    NOTIFICATION 2: 
        INTERFACE: UIInterfaceOrientationPortrait   
        DEVICE: UIDeviceOrientationFaceUp
    

    Still portrait (and not up side down) for the interface, and back to face up for the device (which is correct).

My app adjusts itself correctly, and is shown up side down, which is totally wrong.

What can I do to know the last known orientation, the one's that's shown in the home screen and other UI on the iPad?

This is an OpenGL app with just a window and a view. I've already added a UIViewController to the mix to see if this gives more accurate information, but it's just the same, and wrong. I know I'm not the only dev struggling with this, because many apps I have on my iPad do it wrong too, you have to tilt the device to make them orient themselves properly. But some apps do seem to get it right, what's the secret?

Olatha answered 8/6, 2011 at 23:17 Comment(6)
I know nothing of IPad development, but how would it be physically possible to detect "orientation" when the device is flat?Ethyne
Oli, what I'm after, which I didn't really make clear, is what the last known orientation is, the one that's shown in the iPad UI. I'll add this.Olatha
Are you sure the code displaying the orientations is correct? If the device is flat, it should keep the same orientation (unless your app doesn't support it).Network
iPad users expect an iPad app to open correctly in whatever orientation they are holding the device. Do you need to keep a record of the orientation cache so you can compare it when face up/down are reported?Shipwreck
@ughoavgfhw: this behavior is documented by many (but not by Apple). The successive (re-)orientation events are described here: blogs.remobjects.com/blogs/mh/2010/06/29/p1685 as: "Essentially, the device is pretending the user really quickly shook it back to an upright position, and back down."Olatha
@Krypton: this issue is only affecting my app when it just launches, so there's no way to keep a record, there simply isn't enough data to record.Olatha
O
4

Okay, I'm not happy to have to admit that I did overlook an essential piece in the maze that is iOS device/interface orientations: the UISupportedInterfaceOrientations property in your app's Info.plist file.

I read about it and (wrongly) assumed that by specifying the supported orientations, the behavior wouldn't change. But as it turns out, it's not just a filter on the orientation notifications (it isn't, you still get notifications for unsupported orienations), but the type/order of notifications is different depending on how the device is oriented.

In my case, I wasn't interested in the "face up" and "face down" orientations, so I just ignored them. This is not a good idea. If you specify the supported orientations explicitly:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

...then you will still see the behavior as I describe in the comments above:

"Essentially, the device is pretending the user really quickly shook it back to an upright position, and back down."

...but the first passed orientation isn't always portrait, but the correct orientation instead.

So, in the scenario I describe in my question, the device orientations would be as follows:

AT STARTUP: UIDeviceOrientationFaceUp
NOTIFICATION 1: UIDeviceOrientationPortraitUpsideDown <-- CORRECT!
NOTIFICATION 2: UIDeviceOrientationFaceUp <-- CORRECT, BUT IGNORE

I wouldn't want to go as far and call this a bug on Apple's part, but it's unclear to me why the passed orientation in NOTIFICATION 1 is always portrait when you don't specify UISupportedInterfaceOrientations.

Note that this only applies to the iPad. The home screen UI on iPod Touch and iPhone are always in portrait, so it's easier to know what that orientation was at launch (you guessed it: portrait)...

Olatha answered 10/6, 2011 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.