In my iPhone app I need to detect the current orientation and I have to determine if I’m in portrait or landscape. I use this code:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"portrait");
...
} else {
NSLog(@"landscape");
...
}
Everything is ok when my iPhone is in my hand.
But when i put it on the table and i run the application, the content is displayed on the screen in portrait mode and my code goes to else and NSLog prints landscape.
Is my test incomplete ? How to prevent this case ?
EDIT : the test is performed in my controller viewDidLoad method and my application handle rotation.
UIDevice.orientationis of typeUIDeviceOrientation, which is a superset ofUIInterfaceOrientation. You are probably getting the valueUIDeviceOrientationFaceUp.This underscores that yes, your test is incomplete. You should write something like this:
Then, you’ll know if you if you’ve missed something.