Get Current iOS Device Orientation Even If Device's Orientation Locked

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:
Sample Image

The launching when in landscape with iPhone6 plus, after launch the orientation never changed:
Sample Image

Two different screenshot with the same app, Sample Image

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



Leave a reply



Submit