Skip to content
Open
Show file tree
Hide file tree
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 @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Loading