Search code examples
objective-caccelerometer

How to get iPhones current orientation?


Is there a special method to get iPhones orientation? I don't need it in degrees or radians, I want it to return an UIInterfaceOrientation object. I just need it for an if-else construction like

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}  
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}

Thanks in advance!


Solution

  • This is most likely what you want:

    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    

    You can then use system macros like:

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
    
    }
    

    If you want the device orientation use:

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    

    This includes enumerations like UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown