From 49720a5414b5d1f3bf7dadf5ac351a3ccef5133a Mon Sep 17 00:00:00 2001 From: Mike Hartington Date: Mon, 13 Jul 2020 14:27:10 -0400 Subject: [PATCH] feat(ios): improve initial webview loading appearance (#2933) Improve background appearance of the webview during the initial load and ensure that the launch storyboard background matches the system. Co-authored-by: Ian Keith Co-authored-by: jcesarmobile --- .../App/Base.lproj/LaunchScreen.storyboard | 13 ++++---- .../Capacitor/CAPBridgeViewController.swift | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ios-template/App/App/Base.lproj/LaunchScreen.storyboard b/ios-template/App/App/Base.lproj/LaunchScreen.storyboard index 412ea5f7a..e7ae5d780 100644 --- a/ios-template/App/App/Base.lproj/LaunchScreen.storyboard +++ b/ios-template/App/App/Base.lproj/LaunchScreen.storyboard @@ -1,11 +1,10 @@ - - - - + + - + + @@ -16,6 +15,7 @@ + @@ -25,5 +25,8 @@ + + + diff --git a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift index 4c89f1417..1a6ef6a57 100644 --- a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift +++ b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift @@ -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 @@ -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 } @@ -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() @@ -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) }