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
}

Swift 5

Declaration

var motionManager   : CMMotionManager!

Initialisation

func addCoreMotion() {

    let splitAngle:Double = 0.75
    let updateTimer:TimeInterval = 0.5

    motionManager = CMMotionManager()
    motionManager?.gyroUpdateInterval = updateTimer
    motionManager?.accelerometerUpdateInterval = updateTimer

    var orientationLast    = UIInterfaceOrientation(rawValue: 0)!

    motionManager?.startAccelerometerUpdates(to: (OperationQueue.current)!, withHandler: {
        (acceleroMeterData, error) -> Void in
        if error == nil {

            let acceleration = (acceleroMeterData?.acceleration)!
            var orientationNew = UIInterfaceOrientation(rawValue: 0)!

            if acceleration.x >= splitAngle {
                orientationNew = .landscapeLeft
            }
            else if acceleration.x <= -(splitAngle) {
                orientationNew = .landscapeRight
            }
            else if acceleration.y <= -(splitAngle) {
                orientationNew = .portrait
            }
            else if acceleration.y >= splitAngle {
                orientationNew = .portraitUpsideDown
            }

            if orientationNew != orientationLast && orientationNew != .unknown{
                orientationLast = orientationNew
                self.deviceOrientationChanged(orinetation: orientationNew)
            }
        }
        else {
            print("error : \(error!)")
        }
    })
}

Result

 func deviceOrientationChanged(orinetation:UIInterfaceOrientation) {

      print("orinetation :",orinetation.rawValue)
     // updatYourUI or another stuff

 }