@@ -4,7 +4,11 @@ import WebKit
4
4
var webView : WKWebView ! = nil
5
5
6
6
class ViewController : UIViewController , WKNavigationDelegate , UIDocumentInteractionControllerDelegate {
7
-
7
+ enum LoadingMode {
8
+ case defaultCachePolicy
9
+ case forceCache
10
+ }
11
+
8
12
var documentController : UIDocumentInteractionController ?
9
13
func documentInteractionControllerViewControllerForPreview( _ controller: UIDocumentInteractionController ) -> UIViewController {
10
14
return self
@@ -17,6 +21,7 @@ class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteract
17
21
var toolbarView : UIToolbar !
18
22
19
23
var htmlIsLoaded = false ;
24
+ private var loadingMode = LoadingMode . defaultCachePolicy
20
25
21
26
private var themeObservation : NSKeyValueObservation ?
22
27
var currentWebViewTheme : UIUserInterfaceStyle = . unspecified
@@ -124,8 +129,22 @@ class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteract
124
129
webviewView. addSubview ( toolbarView)
125
130
}
126
131
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
129
148
}
130
149
131
150
func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
@@ -147,19 +166,24 @@ class ViewController: UIViewController, WKNavigationDelegate, UIDocumentInteract
147
166
func webView( _ webView: WKWebView , didFailProvisionalNavigation navigation: WKNavigation ! , withError error: Error ) {
148
167
htmlIsLoaded = false ;
149
168
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 ;
152
174
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 {
155
180
animateConnectionProblem ( true ) ;
156
-
157
181
setProgress ( 0.05 , true ) ;
158
-
182
+
159
183
DispatchQueue . main. asyncAfter ( deadline: . now( ) + 3 ) {
160
184
self . setProgress ( 0.1 , true ) ;
161
185
DispatchQueue . main. asyncAfter ( deadline: . now( ) + 3 ) {
162
- self . loadRootUrl ( ) ;
186
+ self . reloadWebview ( )
163
187
}
164
188
}
165
189
}
0 commit comments