Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 display content if the orientation is locked (as the Youtube app does; it locks to landscape rather than portrait). This must be possible.

like image 900
wombat57 Avatar asked Mar 23 '11 19:03

wombat57


2 Answers

You can use the UIAccelerometer class to determine the phone's orientation in great detail. If the acceleration vector enters a state where its largest absolute component is on the X axis, that is a landscape orientation. In theory this could be used to detect the orientation lock: if, within a few seconds of this happening, the controller does NOT receive a call to shouldRotateToInterfaceOrientation, and the [[UIDevice currentDevice] orientation] property is not in landscape, you could then safely assume the user has the orientation locked.

This complicated, and has a delay because shouldRotateToInterfaceOrientation will be called well after the actual vector has actually entered a landscape region. The whole idea is a bit of a hack, and you should probably reconsider why you really need to present landscape views when the user has a preference not to show them.

like image 103
Yetanotherjosh Avatar answered Nov 16 '22 02:11

Yetanotherjosh


There's no way how to detect if orientation is locked or not. YouTube app doesn't lock to landscape, it just displays movie in landscape orientation, but when you rotate your iPhone, movie rotates as well (if there's no orientation lock).

IOW orientation lock is handled by system, transparent to your application.

If you want to achieve this functionality - just display your view in landscape mode even if the iPhone is in portrait mode and later enable rotation of your view. It will behave in the same way as YouTube app.

Update for comment:

.h

BOOL rotationEnabled;

.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  return rotationEnabled || ( toInterfaceOrientation == UIInterfaceOrientationLandscapeRight );
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  rotationEnabled = YES;
}
like image 27
zrzka Avatar answered Nov 16 '22 02:11

zrzka