Skip to content

Commit

Permalink
feat(ios): improve webview background experience
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartington committed May 15, 2020
1 parent a9d4698 commit 3bcb502
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import WebKit
import Cordova

public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate {

private var webView: WKWebView?

public var bridgedWebView: WKWebView? {
return webView
}

public var bridgedViewController: UIViewController? {
return self
}
Expand All @@ -23,19 +23,19 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
private var allowNavigationConfig: [String]?
private var basePath: String = ""
private let assetsFolder = "public"

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

@objc public var startDir = ""
@objc public var config: String?

// Construct the Capacitor runtime
public var bridge: CAPBridge?
private var handler: CAPAssetHandler?

override public func loadView() {
let configUrl = Bundle.main.url(forResource: "config", withExtension: "xml")
let configParser = XMLParser(contentsOf: configUrl!)!;
Expand All @@ -44,7 +44,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
guard let startPath = self.getStartPath() else {
return
}

setStatusBarDefaults()
setScreenOrientationDefaults()
let capConfig = CAPConfig(self.config)
Expand All @@ -65,7 +65,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
o.add(self, name: "bridge")

webViewConfiguration.userContentController = o

configureWebView(configuration: webViewConfiguration)

if let appendUserAgent = (capConfig.getValue("ios.appendUserAgent") as? String) ?? (capConfig.getValue("appendUserAgent") as? String) {
Expand All @@ -89,15 +89,21 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}
webView?.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
view = webView

setKeyboardRequiresUserInteraction(false)

bridge = CAPBridge(self, o, capConfig, specifiedScheme)

webView?.isOpaque = false
if let backgroundColor = (bridge!.config.getValue("ios.backgroundColor") as? String) ?? (bridge!.config.getValue("backgroundColor") as? String) {
webView?.backgroundColor = UIColor(fromHex: backgroundColor)
webView?.scrollView.backgroundColor = UIColor(fromHex: backgroundColor)
if #available(iOS 13.0, *) {
webView?.backgroundColor = UIColor.systemBackground
} else {
webView?.backgroundColor = UIColor(fromHex: backgroundColor)
};
webView?.scrollView.backgroundColor = UIColor(fromHex: backgroundColor)
}

if let overrideUserAgent = (bridge!.config.getValue("ios.overrideUserAgent") as? String) ?? (bridge!.config.getValue("overrideUserAgent") as? String) {
webView?.customUserAgent = overrideUserAgent
}
Expand Down Expand Up @@ -160,13 +166,13 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr

func printLoadError() {
let fullStartPath = URL(fileURLWithPath: assetsFolder).appendingPathComponent(startDir)

CAPLog.print("⚡️ ERROR: Unable to load \(fullStartPath.relativePath)/index.html")
CAPLog.print("⚡️ This file is the root of your web app and must exist before")
CAPLog.print("⚡️ Capacitor can run. Ensure you've run capacitor copy at least")
CAPLog.print("⚡️ or, if embedding, that this directory exists as a resource directory.")
}

func fatalLoadError() -> Never {
printLoadError()
exit(1)
Expand Down Expand Up @@ -252,7 +258,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DecidePolicyForNavigationAction.name()), object: navigationAction)
let navUrl = navigationAction.request.url!

/*
* Give plugins the chance to handle the url
*/
Expand All @@ -274,7 +280,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}
}
}

if let allowNavigation = allowNavigationConfig, let requestHost = navUrl.host {
for pattern in allowNavigation {
if matchHost(host: requestHost, pattern: pattern.lowercased()) {
Expand Down Expand Up @@ -476,9 +482,9 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}

public func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {

let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)

alertController.addTextField { (textField) in
textField.text = defaultText
}
Expand All @@ -493,9 +499,9 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}))

alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in

completionHandler(nil)

}))

self.present(alertController, animated: true, completion: nil)
Expand Down Expand Up @@ -547,7 +553,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr

/**
* Add hooks to detect failed HTTP requests
func webView(webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: NSError) {
Expand All @@ -560,5 +566,5 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}
}
*/

}

0 comments on commit 3bcb502

Please sign in to comment.