我有一个基本的WkWebView
,但希望有两个不同的类可以访问它。
ViewController.swift
class ViewController: UIViewController, UIWebViewDelegate, WKNavigationDelegate, WKScriptMessageHandler {
var webView = WKWebView()
override func viewDidLoad() {
webView = WKWebView(frame: self.view.frame)
self.view.addSubview(webView)
}
}
SwiftJavascriptBridge.swift
class SwiftJavascriptBridge: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
//How can I access webView here?
}
正如您所看到的,我在webView
中创建了ViewController
变量,但也想从我的SwiftJavascriptBridge.swift
访问它。我怎样才能实现这一点,以便两个类都可以编辑我的Web视图?
答案 0 :(得分:1)
class WKWebViewFactory {
let webView = WKWebView()
static var sharedInstance = WKWebViewFactory()
}
然后以WKWebViewFactory.sharedInstance.webView