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

feat(ios): improve webview background experience #2933

Merged
merged 12 commits into from
Jul 13, 2020
13 changes: 8 additions & 5 deletions ios-template/App/App/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17132" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17105"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -16,6 +15,7 @@
<imageView key="view" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Splash" id="snD-IY-ifK">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</imageView>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand All @@ -25,5 +25,8 @@
</scenes>
<resources>
<image name="Splash" width="1366" height="1366"/>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
30 changes: 30 additions & 0 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
private var basePath: String = ""
private let assetsFolder = "public"

private enum WebViewLoadingState {
case unloaded
case initialLoad(isOpaque: Bool)
case subsequentLoad
}
private var webViewLoadingState = WebViewLoadingState.unloaded

private var isStatusBarVisible = true
private var statusBarStyle: UIStatusBarStyle = .default
private var statusBarAnimation: UIStatusBarAnimation = .slide
Expand Down Expand Up @@ -97,7 +104,12 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
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)
} else if #available(iOS 13, *) {
// Use the system background colors if background is not set by user
webView?.backgroundColor = UIColor.systemBackground
webView?.scrollView.backgroundColor = UIColor.systemBackground
}

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

func loadWebView() {
// Set the webview to be not opaque on the inital load. This prevents
// the webview from showing a white background, which is its default
// loading display, as that can appear as a screen flash. This might
// have already been set by something else, like a plugin, so we want
// to save the current value to reset it on success or failure.
if let webView = webView, case .unloaded = webViewLoadingState {
webViewLoadingState = .initialLoad(isOpaque: webView.isOpaque)
webView.isOpaque = false
}

let fullStartPath = URL(fileURLWithPath: assetsFolder).appendingPathComponent(startDir).appendingPathComponent("index")
if Bundle.main.path(forResource: fullStartPath.relativePath, ofType: "html") == nil {
fatalLoadError()
Expand Down Expand Up @@ -298,10 +320,18 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
}

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if case .initialLoad(let isOpaque) = webViewLoadingState {
webView.isOpaque = isOpaque
webViewLoadingState = .subsequentLoad
}
CAPLog.print("⚡️ WebView loaded")
}

public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
if case .initialLoad(let isOpaque) = webViewLoadingState {
webView.isOpaque = isOpaque
webViewLoadingState = .subsequentLoad
}
CAPLog.print("⚡️ WebView failed to load")
CAPLog.print("⚡️ Error: " + error.localizedDescription)
}
Expand Down