How Do I detect the orientation of the device on iOS?

I have a question on how to detect the device orientation on iOS. I don’t need to receive change notifications, just the current orientation itself. This seems to be a rather simple question, but I haven’t been able to wrap my head around it. Below is what I have done so far:

UIDevice *myDevice = [UIDevice currentDevice] ;
[myDevice beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation deviceOrientation = myDevice.orientation;
BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
[myDevice endGeneratingDeviceOrientationNotifications];

In my mind this should work. I enable the device to receive device orientation notices, then ask for what orientation it is in, but then it is not working and I don’t know why.

Detect iOS device orientation lock

How can I programmatically check whether the device orientation is locked in iOS? I see nothing in UIDevice. I know that this is supposed to be transparent to the app. But I’d like to change the way I

Detecting initial device orientation on iOS in Safari

I know how to detect device orientation change on iOS in Safari, but I cannot fathom out how to detect initial orientation when my page loads. It is simple to detect if the device is in portrait or la

How do I detect if my iOS device has an LED light or not?

How do I detect if my iOS device has an LED light or not? I am trying to see the difference between an iPad 2 (front and back camera) and an iPhone 4 (front, back + LED light)

how to detect orientation of android device?

Is it possible simple to detect current orientation of android device, without programming a listener and handling the position matrix? In my app I want only to know current orientation – vertical or

How do I programmatically create views based on device orientation in iOS?

I want to create many view’s programmatically and set their size based on the device orientation. This is how I am doing it now: UIView *view1 = [[UIView alloc] init]; if(UIInterfaceOrientationIsLands

Ios – Detect device orientation in background mode

My app is running in background mode because I’m playing and recording audio. I wish to find the device orientation while I’m recording. I set up notification that tells me when the orientation change

How to detect Device Orientation before viewDidLoad?

I need to detect device orientation before running viewDidLoad method in controller. I tried all solutions purposed in other questions but I can’t solve. What’s the best way using SotryBoard in xCode

When an iOS device rotates, how do I change the orientation only one element?

I would like to be able to handle orientation changes in my current iPhone application. The problem is that I don’t want the view of my controller to rotate or resize. When the device orientation chan

Auto Detect Device Orientation

How to achieve the following functionalities in iphone app. Always app launch portrait mode. if the simulator is landscape mode first launch in portrait mode then detect the device orientation change

How can I detect if UI orientation is locked or not on iOS?

Is it possible to detect UI orientation is locked or not currently? If so, how can I do that? I want to do something with gravity sensor when only UI orientation is not locked.

Answers

UIViewController has an interfaceOrientation property that you can access to find out the current orientation of a view controller.

As for your example, that should work. When you say it isn’t working, what do you mean? What results does it give you versus what you expected?

Have you unlocked the hardware lock for device orientation? There is one at the edge of my iPad 1.

Really old thread, but no real solution.

I Had the same problem, but found out that getting The UIDeviceOrientation isn’t always consistent, so instead use this:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) //Default orientation 
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
else if(orientation == UIInterfaceOrientationPortrait)
    //Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
    // Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
    //Do something if right

if UIViewController:

if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
    // 
}

if UIView:

if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
    //
}

UIDevice.h:

#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)

Updated:

add this code to xxx-Prefix.pch then you can use it anywhere:

// check device orientation
#define dDeviceOrientation [[UIDevice currentDevice] orientation]
#define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
#define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
#define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO

usage:

if (isLandscape) { NSLog(@"Landscape"); }

Wasn’t satisfied by “UIDeviceOrientation” because when a UIViewcontroller orientation is fixed to a specific orientation you don’t get a pertinent information with the device orientation, so the right thing to do is using “UIInterfaceOrientation”.

You can get the orientation from the UIViewController with a “self.interfaceOrientation”, but when you are factorizing our code, you might need to do this kind of test outside a view controller, (custom view, a category…), so you still can access the information anywhere outside the controller by using the rootviewController:

if (UIInterfaceOrientationIsLandscape(view.window.rootViewController.interfaceOrientation)) {
}

For what You looking for first you have to Get Notification if Orientation Changed! You Can set This Thing in viewDidLoad like

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

and whenever Orientation of your Device changed OrientationDidChange Called where You can do whatever You Want as Per Orientation

-(void)OrientationDidChange:(NSNotification*)notification
{
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
    }
}

If you want to get device orientation directly from accelerometer use [[UIDevice currentDevice] orientation]. But if you need current orientation of your application(interface orientation) use [[UIApplication sharedApplication] statusBarOrientation].

There’s a way to achieve this whether the orientation lock is enabled or not by using data from CoreMotion. This is the code:

#import <CoreMotion/CoreMotion.h> 

    CMMotionManager *cm=[[CMMotionManager alloc] init];
    cm.deviceMotionUpdateInterval=0.2f;
    [cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                            withHandler:^(CMDeviceMotion *data, NSError *error) {

                            if(fabs(data.gravity.x)>fabs(data.gravity.y)){
                                    NSLog(@"LANSCAPE");
                                if(data.gravity.x>=0){
                                    NSLog(@"LEFT");
                                }
                                else{
                                    NSLog(@"RIGHT");
                                }

                        }
                        else{
                                NSLog(@"PORTRAIT");
                                if(data.gravity.y>=0){
                                    NSLog(@"DOWN");
                                }
                                else{

                                    NSLog(@"UP");
                                }

                            }

}];

Here is some Swift variables to make detection easier:

let LANDSCAPE_RIGHT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight
let LANDSCAPE_LEFT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft
let LANDSCAPE: Bool = LANDSCAPE_LEFT || LANDSCAPE_RIGHT
let PORTRAIT_NORMAL: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait
let PORTRAIT_REVERSE: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown
let PORTRAIT: Bool = PORTRAIT_REVERSE || PORTRAIT_NORMAL

In Swift 3.0

to get device orientation.

/* return current device orientation.
   This will return UIDeviceOrientationUnknown unless device orientation notifications are being generated. 
*/
UIDevice.current.orientation

to get device orientation from your app

UIApplication.shared.statusBarOrientation