How can I get the current screen orientation?

Hello,

I am working on an iOS app that has interactive components that react to the device accelerometer. The app works in landscape left and right only, and I need some way to identify if the screen is left or right to get the acceleration direction correct. Basically, accelerating to the device's "left" means you actually need to move the elements on screen to the left or right depending on screen direction.

One solution I tried is using UIDeviceOrientation. This works in most cases, but when screen lock is on, it gives an update about the device orientation when the screen itself didn't tilt.

Is there some other way to get the screen orientation that accurately reflects the screen orientation and not just device orientation?

Thank you.

Not sure this is what you want, maybe it can help

private func setupOrientationNotification() { cancellableOrientation = NotificationCenter.default .publisher(for: UIDevice.orientationDidChangeNotification) .compactMap { _ in UIDevice.current.orientation } .sink { newOrientation in if newOrientation == .faceUp || newOrientation == .faceDown { // do nothing } else { self.orientation = newOrientation orientation = newOrientation } } } private var cancellableOrientation: AnyCancellable? = nil @Published var orientation

I wrote (copied) this months ago, don't ask me to explain it.

I appreciate the answer, but this is unfortunately the same solution I am using right now. It has no way to know if the screen itself rotated, only if the device rotated.

How can I get the current screen orientation?
 
 
Q