diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift index 83e07696a..26ee03a8d 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift @@ -29,17 +29,6 @@ struct WebReader: UIViewRepresentable { let webView = WebViewManager.shared() let contentController = WKUserContentController() - webView.loadHTMLString( - WebReaderContent( - articleContent: articleContent, - item: item, - isDark: UITraitCollection.current.userInterfaceStyle == .dark, - fontSize: fontSize() - ) - .styledContent, - baseURL: ViewsPackage.bundleURL - ) - webView.navigationDelegate = context.coordinator webView.isOpaque = false webView.backgroundColor = .clear @@ -63,6 +52,7 @@ struct WebReader: UIViewRepresentable { context.coordinator.linkHandler = openLinkAction context.coordinator.webViewActionHandler = webViewActionHandler context.coordinator.updateNavBarVisibilityRatio = navBarVisibilityRatioUpdater + loadContent(webView: webView) return webView } @@ -82,5 +72,39 @@ struct WebReader: UIViewRepresentable { context.coordinator.previousDecreaseFontActionID = decreaseFontActionID (webView as? WebView)?.decreaseFontSize() } + + // If the webview had been terminated `needsReload` will have been set to true + if context.coordinator.needsReload { + loadContent(webView: webView) + context.coordinator.needsReload = false + return + } + + if webView.isLoading { return } + + // If the root element is not detected then `WKWebView` may have unloaded the content + // so we need to load it again. + webView.evaluateJavaScript("document.getElementById('root') ? true : false") { hasRootElement, _ in + guard let hasRootElement = hasRootElement as? Bool else { return } + + if !hasRootElement { + DispatchQueue.main.async { + loadContent(webView: webView) + } + } + } + } + + func loadContent(webView: WKWebView) { + webView.loadHTMLString( + WebReaderContent( + articleContent: articleContent, + item: item, + isDark: UITraitCollection.current.userInterfaceStyle == .dark, + fontSize: fontSize() + ) + .styledContent, + baseURL: ViewsPackage.bundleURL + ) } } diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift index c4ad4d6d5..c75bca135 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift @@ -12,7 +12,7 @@ typealias WKScriptMessageReplyHandler = (Any?, String?) -> Void final class WebReaderCoordinator: NSObject { var webViewActionHandler: (WKScriptMessage, WKScriptMessageReplyHandler?) -> Void = { _, _ in } var linkHandler: (URL) -> Void = { _ in } - var needsReload = true + var needsReload = false var lastSavedAnnotationID: UUID? var previousIncreaseFontActionID: UUID? var previousDecreaseFontActionID: UUID? @@ -69,6 +69,10 @@ extension WebReaderCoordinator: WKNavigationDelegate { webView.backgroundColor = .systemBackground #endif } + + func webViewWebContentProcessDidTerminate(_: WKWebView) { + needsReload = true + } } #if os(iOS)