Files
mowesta/ios-app/app/View/WebView.swift
T
handte ec6f25e11b
LocationBasedServices/mowesta/pipeline/head This commit looks good
Started implementation of iOS app.
2022-05-28 23:33:05 +02:00

105 lines
3.5 KiB
Swift

//
// WebView.swift - Code to make a web view show an
// url in swiftui.
//
// Created by Marcus Handte on 21.06.21.
//
import Foundation
import SwiftUI
import WebKit
import os
/// A web view that shows an url.
struct WebView : UIViewRepresentable {
/// The url request to load
let url: URL;
/// The logger to print debug output.
private let log = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "WebView")
func makeUIView(context: Context) -> WKWebView {
let redirectConsole = """
window.console.log = function(msg) {
window.webkit.messageHandlers.log.postMessage(msg)
}
window.console.error = window.console.log
window.console.debug = window.console.log
"""
let controller = WKUserContentController()
controller.addUserScript(WKUserScript(source: redirectConsole, injectionTime: .atDocumentStart, forMainFrameOnly: true))
let handler = MessageHandler()
controller.add(handler, name: "log")
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = context.coordinator
webView.uiDelegate = context.coordinator
return webView;
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// only load the url again, if the url has changed
if uiView.url != nil {
log.debug("Keeping \(String(describing: url), privacy: .public)")
} else {
log.debug("Loading \(String(describing: url), privacy: .public)")
uiView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
}
}
func makeCoordinator() -> WebViewCoordinator {
return WebViewCoordinator()
}
}
/// A webkit navigation and ui delegate that is used to handle links.
class WebViewCoordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.request.url?.scheme == "file" {
decisionHandler(.allow)
} else {
UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
decisionHandler(.cancel)
}
}
}
/// The message handler that reacts to settings and logout requests.
public class MessageHandler: NSObject, WKScriptMessageHandler {
private let log = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "MessageHandler")
/// Called when a new event shall be dispatched.
///
/// - parameter userContentController: The content controller.
/// - parameter message: The message.
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "log", let msg = message.body as? String {
log.log("Javascript: \(msg, privacy: .public)")
} else {
log.error("Unknown log: \(message.name, privacy: .public) \(String(describing: message.body), privacy: .public)")
}
}
}
#if DEBUG
struct WebView_Previews : PreviewProvider {
@State static var loaded: URLRequest? = nil
static var previews: some View {
WebView(
url: Bundle.main.url(forResource: "privacy_de", withExtension: "html")!
)
}
}
#endif