Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cordova-plugin-screen-orientation support #1062

Merged
merged 1 commit into from
Dec 21, 2018
Merged
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
49 changes: 48 additions & 1 deletion ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr

private var isStatusBarVisible = true
private var statusBarStyle: UIStatusBarStyle = .default
@objc public var supportedOrientations: Array<Int> = []

// Construct the Capacitor runtime
public var bridge: CAPBridge?


override public func loadView() {
setStatusBarDefaults()

setScreenOrientationDefaults()

let webViewConfiguration = WKWebViewConfiguration()

webViewConfiguration.setURLSchemeHandler(CAPAssetHandler(), forURLScheme: CAPBridge.CAP_SCHEME)
Expand Down Expand Up @@ -116,6 +118,30 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}
}

public func setScreenOrientationDefaults() {
if let plist = Bundle.main.infoDictionary {
if let orientations = plist["UISupportedInterfaceOrientations"] as? Array<String> {
for orientation in orientations {
if orientation == "UIInterfaceOrientationPortrait" {
self.supportedOrientations.append(UIInterfaceOrientation.portrait.rawValue)
}
if orientation == "UIInterfaceOrientationPortraitUpsideDown" {
self.supportedOrientations.append(UIInterfaceOrientation.portraitUpsideDown.rawValue)
}
if orientation == "UIInterfaceOrientationLandscapeLeft" {
self.supportedOrientations.append(UIInterfaceOrientation.landscapeLeft.rawValue)
}
if orientation == "UIInterfaceOrientationLandscapeRight" {
self.supportedOrientations.append(UIInterfaceOrientation.landscapeRight.rawValue)
}
}
if self.supportedOrientations.count == 0 {
self.supportedOrientations.append(UIInterfaceOrientation.portrait.rawValue)
}
}
}
}

public func configureWebView(configuration: WKWebViewConfiguration) {
configuration.allowsInlineMediaPlayback = true
configuration.suppressesIncrementalRendering = false
Expand Down Expand Up @@ -375,6 +401,27 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
_ = getWebView().load(request)
}

override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return UIApplication.shared.statusBarOrientation
}

override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
var ret = 0
if self.supportedOrientations.contains(UIInterfaceOrientation.portrait.rawValue) {
ret = ret | (1 << UIInterfaceOrientation.portrait.rawValue)
}
if self.supportedOrientations.contains(UIInterfaceOrientation.portraitUpsideDown.rawValue) {
ret = ret | (1 << UIInterfaceOrientation.portraitUpsideDown.rawValue)
}
if self.supportedOrientations.contains(UIInterfaceOrientation.landscapeRight.rawValue) {
ret = ret | (1 << UIInterfaceOrientation.landscapeRight.rawValue)
}
if self.supportedOrientations.contains(UIInterfaceOrientation.landscapeLeft.rawValue) {
ret = ret | (1 << UIInterfaceOrientation.landscapeLeft.rawValue)
}
return UIInterfaceOrientationMask.init(rawValue: UInt(ret))
}

/**
* Add hooks to detect failed HTTP requests

Expand Down