Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class HybridInterfaceOrientationManager: HybridOrientationManagerSpec {
let source: OrientationSource = .interface
private(set) var currentOrientation: CameraOrientation? = nil
private var observer: NSObjectProtocol? = nil
private var didBecomeActiveObserver: NSObjectProtocol? = nil

override init() {
super.init()
Expand All @@ -34,6 +35,34 @@ final class HybridInterfaceOrientationManager: HybridOrientationManagerSpec {
// Start new listener (beginGeneratingDeviceOrientationNotifications() can be nested)
UIDevice.current.beginGeneratingDeviceOrientationNotifications()

// On a cold launch the window scene is not yet foregroundActive - during
// init() and potentially still at this point - so interfaceOrientation
// resolves to .unknown (treated as portrait), and orientationDidChange
// does not fire until the device physically rotates. An app launched in
// landscape therefore renders all outputs 90° rotated until the first
// rotation. Emit the current orientation now (in case the scene is
// already active), and again when the app becomes active - the moment
// the scene state is guaranteed to be correct.
let emitCurrentOrientation: () -> Void = { [weak self] in
guard let self else { return }
let interfaceOrientation = UIApplication.shared.interfaceOrientation
guard interfaceOrientation != .unknown else { return }
let orientation = CameraOrientation(interfaceOrientation: interfaceOrientation)
if self.currentOrientation != orientation {
logger.info("Interface orientation resolved: \(orientation.stringValue)")
self.currentOrientation = orientation
onChanged(orientation)
}
}
emitCurrentOrientation()
self.didBecomeActiveObserver = NotificationCenter.default.addObserver(
forName: UIApplication.didBecomeActiveNotification,
object: nil,
queue: .main
) { _ in
emitCurrentOrientation()
}

self.observer = NotificationCenter.default.addObserver(
forName: UIDevice.orientationDidChangeNotification,
object: nil,
Expand Down Expand Up @@ -62,6 +91,10 @@ final class HybridInterfaceOrientationManager: HybridOrientationManagerSpec {
NotificationCenter.default.removeObserver(observer)
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
if let didBecomeActiveObserver = self.didBecomeActiveObserver {
NotificationCenter.default.removeObserver(didBecomeActiveObserver)
self.didBecomeActiveObserver = nil
}
}
}
}