问题描述
我正在尝试使用一个带有单个按钮的简单 WKWebView,该按钮在单击时应通过 JavaScript 回调到 Swift。但是,我在 Xcode 控制台中看到以下日志,并且视图加载了空的 WKWebView:
- [pageProxyID=5,webPageID=6,PID=0] WebPageProxy::processDidTerminate: (pid 0),原因 3
- [pageProxyID=5,PID=0] WebPageProxy::dispatchProcessDidTerminate: reason = 3
- [pageProxyID=5,PID=84636] WebPageProxy::processDidTerminate: (pid 84636),PID=84636] WebPageProxy::dispatchProcessDidTerminate: reason = 3
- [pageProxyID=5,PID=84636] WebPageProxy::tryReloadAfterProcesstermination:进程崩溃并且 客户端没有处理它,没有重新加载页面,因为我们 达到最大尝试次数
我初始化 WKWebView 和设置回调的代码如下所示:
override func viewDidLoad() {
super.viewDidLoad()
let config = WKWebViewConfiguration()
config.userContentController.add(self,name: "callback")
wkWebView = WKWebView(frame: self.view.frame,configuration: config)
wkWebView.autoresizingMask = [.height,.width]
self.view.addSubview(wkWebView)
let htmlStr = "<!DOCTYPE html><html><body><button type=\"button\" onclick=\"myFunction()\">My Button</button><script type=\"text/javascript\">function myFunction() { window.webkit.messageHandlers.callback.postMessage('clicked!'); }</script></body></html>"
self.wkWebView.loadHTMLString(htmlStr,baseURL: Bundle.main.resourceURL)
}