From a6ea885e4472988ef0d7858d9d68001756f6b416 Mon Sep 17 00:00:00 2001 From: Andre Destro Date: Thu, 20 Aug 2026 14:23:35 +0100 Subject: [PATCH] chore(screen-orientation): remove unneeded iOS code The plugin's deployment target is iOS 16.0, so the `#available(iOS 16.0, *)` checks always pass and their fallback branches were unreachable: - `lockLegacy(_:)`, which set the orientation through KVC on `UIDevice` and called `attemptRotationToDeviceOrientation()`, deprecated in iOS 16 - the `attemptRotationToDeviceOrientation()` fallback in `unlock` Removing them clears the only deprecation warning in the iOS build. The remaining geometry update was duplicated between `lock` and `unlock`, so it now lives in a single `requestGeometryUpdate(_:completion:)` helper. That also fixes the completion being called twice: `completion(nil)` ran unconditionally right after the request was made, so a failure resolved the call before rejecting it. The error handler only runs when the request is rejected, so its result is now reported once, after the request. --- .../ScreenOrientation.swift | 46 ++++++------------- .../ScreenOrientationPlugin.swift | 3 +- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientation.swift b/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientation.swift index c1e9e1e08..a28d513c4 100644 --- a/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientation.swift +++ b/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientation.swift @@ -21,49 +21,33 @@ public class ScreenOrientation: NSObject { return fromDeviceOrientationToOrientationType(currentOrientation) } - private func lockLegacy(_ orientation: Int) { - UIDevice.current.setValue(orientation, forKey: "orientation") - UINavigationController.attemptRotationToDeviceOrientation() - } - public func lock(_ orientationType: String, completion: @escaping (Error?) -> Void) { DispatchQueue.main.async { let orientation = self.fromOrientationTypeToInt(orientationType) self.capViewController?.supportedOrientations = [orientation] let mask = self.fromOrientationTypeToMask(orientationType) - if #available(iOS 16.0, *) { - if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene { - windowScene.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations() - windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in - completion(error) - } - } else { - completion(ScreenOrientationError.noWindowScene) - } - } else { - self.lockLegacy(orientation) - } - completion(nil) + self.requestGeometryUpdate(mask, completion: completion) } } public func unlock(completion: @escaping (Error?) -> Void) { DispatchQueue.main.async { self.capViewController?.supportedOrientations = self.supportedOrientations - if #available(iOS 16.0, *) { - if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene { - windowScene.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations() - windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .all)) { error in - completion(error) - } - } else { - completion(ScreenOrientationError.noWindowScene) - } - } else { - UINavigationController.attemptRotationToDeviceOrientation() - } - completion(nil) + self.requestGeometryUpdate(.all, completion: completion) + } + } + + private func requestGeometryUpdate(_ mask: UIInterfaceOrientationMask, completion: @escaping (Error?) -> Void) { + guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { + completion(ScreenOrientationError.noWindowScene) + return + } + windowScene.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations() + var requestError: Error? + windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in + requestError = error } + completion(requestError) } private func fromDeviceOrientationToOrientationType(_ orientation: UIDeviceOrientation) -> String { diff --git a/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientationPlugin.swift b/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientationPlugin.swift index 69b0aa932..b098c34df 100644 --- a/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientationPlugin.swift +++ b/screen-orientation/ios/Sources/ScreenOrientationPlugin/ScreenOrientationPlugin.swift @@ -41,16 +41,17 @@ public class ScreenOrientationPlugin: CAPPlugin, CAPBridgedPlugin { implementation.lock(lockToOrientation) { error in if let error = error { call.reject(error.localizedDescription) + return } call.resolve() } - } @objc public func unlock(_ call: CAPPluginCall) { implementation.unlock { error in if let error = error { call.reject(error.localizedDescription) + return } call.resolve() }