I want to query the orientation the iPhone is currently in. Using
[UIDevice currentDevice].orientation
works as long as the device isn't orientation-locked. If it is locked, however, it always responds with the locked orientation, not with the actual orientation of the device.
Is there a high-level way to get the actual device orientation?
Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.
The orientation lock can be quickly accessed within Control Center of iOS device. If you turn this lock to the “ON” state, you can rotate your device from vertical, like a portrait, to horizontal, like a landscape and the screen won’t readjust. In this case, you need to turn your head 90˚ to read it.
If the orientation lock is on the “OFF” state, you can turn your device from portrait to landscape. Your iDevice will re-orient the screen, and it will be readable in horizontal (landscape) mode. Portrait orientation on iPhone is used to lock or unlock the screen orientation, which can be accessed in Control Center.
Portrait orientation on iPhone is used to lock or unlock the screen orientation, which can be accessed in Control Center. When you turn on this function, your iPhone orientation will now stay locked in portrait mode until you turn Portrait Orientation Lock off. You should know that there is no option to lock iPhone screen in landscape mode.
When the screen orientation is locked, appears in the status bar (on supported models ). Helpful? Please don’t include any personal information in your comment. Maximum character limit is 250.
Also you can use CoreMotion
Orientation detection algorithm:
if abs( y ) < abs( x ) your iPhone is in landscape position, look sign of x to detect right or left
else your iPhone is in portrait position, look sign of y to detect up or upside-down.
If you are interested in face-up or down, look value of z.
import CoreMotion
var uMM: CMMotionManager! override func viewWillAppear( p: Bool ) { super.viewWillAppear( p ) uMM = CMMotionManager() uMM.accelerometerUpdateInterval = 0.2 // Using main queue is not recommended. So create new operation queue and pass it to startAccelerometerUpdatesToQueue. // Dispatch U/I code to main thread using dispach_async in the handler. uMM.startAccelerometerUpdatesToQueue( NSOperationQueue() ) { p, _ in if p != nil { println( abs( p.acceleration.y ) < abs( p.acceleration.x ) ? p.acceleration.x > 0 ? "Right" : "Left" : p.acceleration.y > 0 ? "Down" : "Up" ) } } } override func viewDidDisappear( p: Bool ) { super.viewDidDisappear( p ) uMM.stopAccelerometerUpdates() }
That functionality is correct. If it always returned the device orientation, even if it was locked, the orientation changed notifications would fire. This would defeat the purpose of the lock.
To answer your question, there is no way to read the raw values from the accelerometer, without using private APIs.
Edit:
After reviewing the documentation, it seems that the UIAccelerometer class provides this data, even when the orientation is locked. This change was applied in iOS 4 and above. Even though you can use this data, you still need to process it to determine the orientation. This is not an easy task as you need to monitor the changes constantly and compare them to older values.
Also, take a look at this guide for handling motion events. This may provide you with another route to determining the orientation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With