-2

I need to know programmatically if "portrait orientation lock" option from device setting has been set. any help would be appreciated.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
crasher
  • 35
  • 1
  • 4

1 Answers1

1

There's no API to tell you if orientation lock is on, however, you can infer it.

The first thing you'll need to do is use the accelerometer to determine which way the device is being held:

CMMotionManager * motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 0.5;

[motionManager startAccelerometerUpdatesToQueue: [[NSOperationQueue alloc] init]
                                    withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                                        CMAcceleration acceleration = accelerometerData.acceleration;

                                        UIDeviceOrientation orientation = fabs(acceleration.y) < fabs(acceleration.x) ?
                                        acceleration.x > 0 ? UIDeviceOrientationLandscapeRight  :   UIDeviceOrientationLandscapeLeft :
                                        acceleration.y > 0 ? UIDeviceOrientationPortraitUpsideDown : UIDeviceOrientationPortrait;

                  // Check orientation == [UIDevice currentDevice].orientation

                                         }];

Then check the orientation you calculated is the same as the current UIDeviceOrientation.

If they're different then orientation lock is on. If they're the same, you can't assume it's either on or off.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160