All,
When i locked the screen orientation,i want use [[UIDevice currentDevice] orientation] to get screen's orientation ,but it always return UIDeviceOrientationPortrait, so, question is how can i get the real Device's orientation.
thank you very much.
-
Did you test this on simulator or device ? I believe this will give exact result on real device only. – Janak Nirmal Jun 23 '11 at 05:19
-
Go through this link http://stackoverflow.com/questions/930075/uidevice-orientation – Naveen Thunga Jun 23 '11 at 05:23
-
You can refer this [so called question][1] and get your problem resolved. [1]:http://stackoverflow.com/questions/634745/iphone-orientation Hope it helps. – Janak Nirmal Jun 23 '11 at 05:25
4 Answers
Make sure you're enabling orientation notifications via:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
If that still doesn't work, you can also use the status bar to figure out the current orientation (if your UI is rotating) via:
[[UIApplication sharedApplication] statusBarOrientation];

- 956
- 4
- 7
Another solution could be detect orientation from CoreMotion library. It would fix a bug when Portrait Orientation Lock: On. Refer that article: http://codrspace.com/yuvirajsinh/device-orientation-detection-using-coremotion/
If you're testing in the Simulator then be aware that the Simulator is terrible at returning accurate orientation values. Test on a device.

- 4,406
- 2
- 23
- 30
You need to turn on orientation notification generation first, as per the documentation.
You need to call:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Then you can check the device's orientation. You need to tell the device that you are done getting the orientation, to conserve battery. You do so by calling:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Personally, I've had better luck with using the status bar's orientation. This is because sometimes, (when the device is flat on a surface, for example) the device orientation won't be "Landscape" or "Portrait".

- 57,511
- 78
- 272
- 425
-
[[UIDevice currentDevice] orientation] does not work if the user has locked his device orientation from the Springboard. orientation is always the last value, and there are no notifications for changes. – Jeff Jul 16 '11 at 01:36