2020-02-26 00:10:51 +01:00
|
|
|
//
|
|
|
|
// PreloadedWebView.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 2/25/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WebKit
|
|
|
|
|
|
|
|
class PreloadedWebView: WKWebView {
|
|
|
|
|
|
|
|
private var isReady: Bool = false
|
2020-09-02 01:54:46 +02:00
|
|
|
private var readyCompletion: (() -> Void)?
|
2020-02-26 00:10:51 +01:00
|
|
|
|
|
|
|
init(articleIconSchemeHandler: ArticleIconSchemeHandler) {
|
|
|
|
let preferences = WKPreferences()
|
|
|
|
preferences.javaScriptCanOpenWindowsAutomatically = false
|
|
|
|
preferences.javaScriptEnabled = true
|
|
|
|
|
|
|
|
let configuration = WKWebViewConfiguration()
|
|
|
|
configuration.preferences = preferences
|
|
|
|
configuration.setValue(true, forKey: "allowUniversalAccessFromFileURLs")
|
|
|
|
configuration.allowsInlineMediaPlayback = true
|
2020-05-10 19:21:35 +02:00
|
|
|
configuration.mediaTypesRequiringUserActionForPlayback = .audio
|
2020-02-26 00:10:51 +01:00
|
|
|
configuration.setURLSchemeHandler(articleIconSchemeHandler, forURLScheme: ArticleRenderer.imageIconScheme)
|
|
|
|
|
|
|
|
super.init(frame: .zero, configuration: configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func preload() {
|
2020-03-17 23:18:24 +01:00
|
|
|
navigationDelegate = self
|
|
|
|
loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL)
|
2020-02-26 00:10:51 +01:00
|
|
|
}
|
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
func ready(completion: @escaping () -> Void) {
|
2020-02-26 00:10:51 +01:00
|
|
|
if isReady {
|
|
|
|
completeRequest(completion: completion)
|
|
|
|
} else {
|
|
|
|
readyCompletion = completion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: WKScriptMessageHandler
|
|
|
|
|
2020-03-17 23:18:24 +01:00
|
|
|
extension PreloadedWebView: WKNavigationDelegate {
|
2020-02-26 00:10:51 +01:00
|
|
|
|
2020-03-17 23:18:24 +01:00
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
|
isReady = true
|
|
|
|
if let completion = readyCompletion {
|
|
|
|
completeRequest(completion: completion)
|
|
|
|
readyCompletion = nil
|
2020-02-26 00:10:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-17 23:18:24 +01:00
|
|
|
|
2020-02-26 00:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension PreloadedWebView {
|
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
func completeRequest(completion: @escaping () -> Void) {
|
2020-02-26 00:10:51 +01:00
|
|
|
isReady = false
|
2020-03-17 23:18:24 +01:00
|
|
|
navigationDelegate = nil
|
2020-09-02 01:54:46 +02:00
|
|
|
completion()
|
2020-02-26 00:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|