The iOS app opens offline: the page is kept however it was loaded (#506)
The phone in airplane mode opened to "Cannot reach ipodderx.sdf1.net". The service worker kept the page only when a navigation passed through it, and the app's own load from Swift may not, so it had no page to answer with. The page now asks the worker to keep / on every load, and the app, when a load fails for want of a connection, tries once more from the web view's cache (returnCacheDataDontLoad) before the alert. The Swift is not compiled here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RqkrVCCH8YcP2Y3i8VNYWA
This commit is contained in:
@@ -136,8 +136,27 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKUIDeleg
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
/// The load from the web view's own cache has been tried since the last that worked.
|
||||
private var triedCache = false
|
||||
|
||||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||
triedCache = false
|
||||
}
|
||||
|
||||
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!,
|
||||
withError error: Error) {
|
||||
// No connection: the page as last loaded, from the web view's cache, which then reads
|
||||
// what the service worker kept. The app's own load may not go through the worker, so
|
||||
// offline it failed and said the server could not be reached (#506).
|
||||
let offline: Set<URLError.Code> = [.notConnectedToInternet, .cannotConnectToHost, .cannotFindHost,
|
||||
.timedOut, .networkConnectionLost, .dnsLookupFailed,
|
||||
.dataNotAllowed, .internationalRoamingOff]
|
||||
if !triedCache, let code = (error as? URLError)?.code, offline.contains(code), let url = Server.url {
|
||||
triedCache = true
|
||||
webView.load(URLRequest(url: url, cachePolicy: .returnCacheDataDontLoad))
|
||||
return
|
||||
}
|
||||
triedCache = false
|
||||
let host = Server.url?.host ?? "the server"
|
||||
let alert = UIAlertController(title: "Cannot reach \(host)",
|
||||
message: error.localizedDescription, preferredStyle: .alert)
|
||||
|
||||
Reference in New Issue
Block a user