Skip to content

Commit 2e1da72

Browse files
authored
Merge pull request #79 from pwa-builder/bugfix-4831-change-cache-strategy-in-network-error
Attempting to change the cache strategy when there is a network error #4831
2 parents e0d3c90 + 79bc8c2 commit 2e1da72

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

Microsoft.PWABuilder.IOS.Web/Resources/ios-project-src/pwa-shell/ViewController.swift

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import WebKit
44
var webView: WKWebView! = nil
55

66
class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteractionControllerDelegate {
7-
7+
enum LoadingMode {
8+
case defaultCachePolicy
9+
case forceCache
10+
}
11+
812
var documentController: UIDocumentInteractionController?
913
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
1014
return self
@@ -17,6 +21,7 @@ class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteract
1721
var toolbarView: UIToolbar!
1822

1923
var htmlIsLoaded = false;
24+
private var loadingMode = LoadingMode.defaultCachePolicy
2025

2126
private var themeObservation: NSKeyValueObservation?
2227
var currentWebViewTheme: UIUserInterfaceStyle = .unspecified
@@ -124,8 +129,22 @@ class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteract
124129
webviewView.addSubview(toolbarView)
125130
}
126131

127-
@objc func loadRootUrl() {
128-
PWAShell.webView.load(URLRequest(url: SceneDelegate.universalLinkToLaunch ?? SceneDelegate.shortcutLinkToLaunch ?? rootUrl))
132+
@objc func loadRootUrl(cachePolicy: NSURLRequest.CachePolicy = .useProtocolCachePolicy) {
133+
CloudpilotEmu.webView.load(URLRequest(url: SceneDelegate.universalLinkToLaunch ?? SceneDelegate.shortcutLinkToLaunch ?? rootUrl, cachePolicy: cachePolicy))
134+
}
135+
136+
func reloadWebview(
137+
loadingMode: LoadingMode = LoadingMode.defaultCachePolicy
138+
) {
139+
switch loadingMode {
140+
case LoadingMode.defaultCachePolicy:
141+
loadRootUrl(cachePolicy: .useProtocolCachePolicy);
142+
143+
case LoadingMode.forceCache:
144+
loadRootUrl(cachePolicy: .useProtocolCachePolicy);
145+
}
146+
147+
self.loadingMode = loadingMode
129148
}
130149

131150
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
@@ -147,19 +166,24 @@ class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteract
147166
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
148167
htmlIsLoaded = false;
149168

150-
if (error as NSError)._code != (-999) {
151-
self.overrideUIStyle(toDefault: true);
169+
if (error as NSError)._code == (-999) { return }
170+
171+
self.overrideUIStyle(toDefault: true);
172+
webView.isHidden = true;
173+
loadingView.isHidden = false;
152174

153-
webView.isHidden = true;
154-
loadingView.isHidden = false;
175+
if loadingMode == LoadingMode.defaultCachePolicy {
176+
DispatchQueue.main.async {
177+
self.reloadWebview(loadingMode: LoadingMode.forceCache)
178+
}
179+
} else {
155180
animateConnectionProblem(true);
156-
157181
setProgress(0.05, true);
158-
182+
159183
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
160184
self.setProgress(0.1, true);
161185
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
162-
self.loadRootUrl();
186+
self.reloadWebview()
163187
}
164188
}
165189
}

0 commit comments

Comments
 (0)