目前我有一个使用WKWebView和Swift渲染的WebView。我已经添加了所有代理,但没有一个被调用。我处理不正确吗?我确实添加了WKScriptMessageHandler,但它们都没有触发onavascript javascript调用。
我的快速代码如下:
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
print("here - \(message)");
}
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {
print("here - \(message)");
}
func webView(webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: (String?) -> Void) {
print("here - \(prompt)");
}
func webView(webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (Bool) -> Void) {
let alert = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: UIAlertActionStyle.Default, handler: { (UIAlertAction ) -> Void in
completionHandler(false);
}));
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction ) -> Void in
UIApplication.sharedApplication().openURL(NSURL(string: (frame.request.URL?.host)!)!);
completionHandler(true);
}));
self.presentViewController(alert, animated: true, completion: nil);
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {}
和javascript代码:
<a href="javascript:void(0);" onclick="open_agree();" title="Agree " tabindex="0">Agree </a>
更新:以下代码对我有用....
func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?