Get current iOS device orientation even if device's orientation locked
Declare motion manger with core motion
var orientationLast = UIInterfaceOrientation(rawValue: 0)!
var motionManager: CMMotionManager?
Motion manager initialisation
and call this function in viewDidLoad
func initializeMotionManager() {
motionManager = CMMotionManager()
motionManager?.accelerometerUpdateInterval = 0.2
motionManager?.gyroUpdateInterval = 0.2
motionManager?.startAccelerometerUpdates(to: (OperationQueue.current)!, withHandler: {
(accelerometerData, error) -> Void in
if error == nil {
self.outputAccelertionData((accelerometerData?.acceleration)!)
}
else {
print("\(error!)")
}
})
}
To analyis accelerometers meter data
func outputAccelertionData(_ acceleration: CMAcceleration) {
var orientationNew: UIInterfaceOrientation
if acceleration.x >= 0.75 {
orientationNew = .landscapeLeft
print("landscapeLeft")
}
else if acceleration.x <= -0.75 {
orientationNew = .landscapeRight
print("landscapeRight")
}
else if acceleration.y <= -0.75 {
orientationNew = .portrait
print("portrait")
}
else if acceleration.y >= 0.75 {
orientationNew = .portraitUpsideDown
print("portraitUpsideDown")
}
else {
// Consider same as last time
return
}
if orientationNew == orientationLast {
return
}
orientationLast = orientationNew
}
iOS device orientation disregarding orientation lock
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.
Track device orientation when orientation is locked
Use the accelerometer to detect device orientation yourself. You can use the Core Motion framework to get the data you need. There's a sample snippet in the linked docs that shows how to get the data. Use a low-pass filter to isolate the force of gravity from relatively short-term changes due to user movement. Apple has a sample project called AccelerometerGraph that demonstrates this.
In Swift, how to get the device orientation correctly right after it's launched?
I have tested many times about orientation, so I have summed up some experience.
In all iPhone devices, except iPhone6(s) plus, the only interface orientation is .portrait
. If App is launched in landscape mode, there must be a change of orientation. One will receive the UIDeviceOrientationDidChangeNotification
. It's an appropriate time to get the orientation.
Regarding the launching when in landscape with iPhone6, the orientation after the launch will change once:
The launching when in landscape with iPhone6 plus, after launch the orientation never changed:
Two different screenshot with the same app,
So before the app does change orientation, the orientation is still like in the home page.
In viewDidLoad
, the orientation has not changed yet, the log will be the wrong direction.
Related Topics
Peripheral and Central at the Same Time on iOS
Get Tapped Word from Uitextview in Swift
Detect Which App Is Currently Running on iOS Using Sysctl
iOS Uicollectionview - Default Flow, Fill Rows from Right to Left
How to Animate an Svg Path in iOS
Sqlite Database on Phonegap/iOS - More Than 5Mb Possible
The Simulator Can't Be Launched Because It Is Already in Use
Invalid Redeclaration in Auto Code Generate Nsmanagedobject Subclass Swift 3
Keep Getting Error Messages When Compiling Newest Version of Admob on iOS6 Sdk
Always Stop in App Delegate After Enabling All Exceptions Break Point
How to Detect If the Currently Running App Was Installed from the App Store
How to Prevent Initial White Flash When Showing a Uiwebview
Xcode 4.3:Missing Icons for iOS Apps in Organizer's Archives
Uitableview Not Visible the Last Cell When Scroll Down
Core Data: Delete All Objects of an Entity Type, Ie Clear a Table
Removing Object from Array in Swift 3
Object-C/iOS :How to Use Asynchronous to Get a Data from Url