import UIKit
import SwiftUI
// Add WebKit
import WebKit
// Add WKNavigationDelegate, WKUIDelegate for navigation and ui delegation
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
// This gets called when loading the WKWebView
override func loadView() {
super.loadView()
webView = WKWebView()
let webViewCofig = WKWebViewConfiguration()
webViewCofig.dataDetectorTypes = []
webViewCofig.allowsInlineMediaPlayback = true
webViewCofig.mediaTypesRequiringUserActionForPlayback = []
webView = WKWebView(frame: view.frame, configuration: webViewCofig)
webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
}
// Called after the WebView was created
override func viewDidLoad() {
super.viewDidLoad()
let htmlPath = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www")!
webView.loadFileURL(htmlPath, allowingReadAccessTo: htmlPath)
webView.navigationDelegate = self
webView.uiDelegate = self
view = webView
}
// Called after HTML was loaed
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// This will output the content of BODY in XCODE debug (to prove its called after canvas was drawn)
webView.evaluateJavaScript("document.body.innerHTML") { value, error in
print(value as? String ?? "")
}
// Simulating click() to body... but didn't help
webView.evaluateJavaScript("document.body.click()") {value, error in print(error ?? "")
}
//webView.stopLoading()
// If you uncomment this, MZ will display something like
// Failed to load
// js/libs/effekseer.wasm
}
// This is to allow Alert()
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
// alert
let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { _ in
completionHandler()
}
alertController.addAction(action)
present(alertController, animated: true, completion: nil)
}
/* Below this is the default code */
// MARK: UIDocumentBrowserViewControllerDelegate