Skip to content

Commit 49720a5

Browse files
mhartingtonikeithjcesarmobile
authored
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 <iankeith@gmail.com> Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
1 parent 0d5e132 commit 49720a5

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
3-
<device id="retina4_7" orientation="portrait">
4-
<adaptation id="fullscreen"/>
5-
</device>
2+
<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">
3+
<device id="retina4_7" orientation="portrait" appearance="light"/>
64
<dependencies>
75
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17105"/>
7+
<capability name="System colors in document resources" minToolsVersion="11.0"/>
98
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
109
</dependencies>
1110
<scenes>
@@ -16,6 +15,7 @@
1615
<imageView key="view" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Splash" id="snD-IY-ifK">
1716
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1817
<autoresizingMask key="autoresizingMask"/>
18+
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
1919
</imageView>
2020
</viewController>
2121
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
@@ -25,5 +25,8 @@
2525
</scenes>
2626
<resources>
2727
<image name="Splash" width="1366" height="1366"/>
28+
<systemColor name="systemBackgroundColor">
29+
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
30+
</systemColor>
2831
</resources>
2932
</document>

ios/Capacitor/Capacitor/CAPBridgeViewController.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
2424
private var basePath: String = ""
2525
private let assetsFolder = "public"
2626

27+
private enum WebViewLoadingState {
28+
case unloaded
29+
case initialLoad(isOpaque: Bool)
30+
case subsequentLoad
31+
}
32+
private var webViewLoadingState = WebViewLoadingState.unloaded
33+
2734
private var isStatusBarVisible = true
2835
private var statusBarStyle: UIStatusBarStyle = .default
2936
private var statusBarAnimation: UIStatusBarAnimation = .slide
@@ -97,7 +104,12 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
97104
if let backgroundColor = (bridge!.config.getValue("ios.backgroundColor") as? String) ?? (bridge!.config.getValue("backgroundColor") as? String) {
98105
webView?.backgroundColor = UIColor(fromHex: backgroundColor)
99106
webView?.scrollView.backgroundColor = UIColor(fromHex: backgroundColor)
107+
} else if #available(iOS 13, *) {
108+
// Use the system background colors if background is not set by user
109+
webView?.backgroundColor = UIColor.systemBackground
110+
webView?.scrollView.backgroundColor = UIColor.systemBackground
100111
}
112+
101113
if let overrideUserAgent = (bridge!.config.getValue("ios.overrideUserAgent") as? String) ?? (bridge!.config.getValue("overrideUserAgent") as? String) {
102114
webView?.customUserAgent = overrideUserAgent
103115
}
@@ -173,6 +185,16 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
173185
}
174186

175187
func loadWebView() {
188+
// Set the webview to be not opaque on the inital load. This prevents
189+
// the webview from showing a white background, which is its default
190+
// loading display, as that can appear as a screen flash. This might
191+
// have already been set by something else, like a plugin, so we want
192+
// to save the current value to reset it on success or failure.
193+
if let webView = webView, case .unloaded = webViewLoadingState {
194+
webViewLoadingState = .initialLoad(isOpaque: webView.isOpaque)
195+
webView.isOpaque = false
196+
}
197+
176198
let fullStartPath = URL(fileURLWithPath: assetsFolder).appendingPathComponent(startDir).appendingPathComponent("index")
177199
if Bundle.main.path(forResource: fullStartPath.relativePath, ofType: "html") == nil {
178200
fatalLoadError()
@@ -298,10 +320,18 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
298320
}
299321

300322
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
323+
if case .initialLoad(let isOpaque) = webViewLoadingState {
324+
webView.isOpaque = isOpaque
325+
webViewLoadingState = .subsequentLoad
326+
}
301327
CAPLog.print("⚡️ WebView loaded")
302328
}
303329

304330
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
331+
if case .initialLoad(let isOpaque) = webViewLoadingState {
332+
webView.isOpaque = isOpaque
333+
webViewLoadingState = .subsequentLoad
334+
}
305335
CAPLog.print("⚡️ WebView failed to load")
306336
CAPLog.print("⚡️ Error: " + error.localizedDescription)
307337
}

0 commit comments

Comments
 (0)