WKUIDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(WKNavigationActionPolicy.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
decisionHandler(WKNavigationResponsePolicy.allow)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
}
WKUIDelegate
iOS8的几个方法(iOS10略)
@available(iOS 8.0, *)
optional public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration , for navigationAction: WKNavigationAction , windowFeatures: WKWindowFeatures) -> WKWebView?
@available(iOS 9.0, *)
optional public func webViewDidClose(_ webView: WKWebView)
@available(iOS 8.0, *)
optional public func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Swift.Void)
@available(iOS 8.0, *)
optional public func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String , initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Swift.Void)
@available(iOS 8.0, *)
optional public func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String , defaultText: String? , initiatedByFrame frame: WKFrameInfo , completionHandler: @escaping (String?) -> Swift.Void )
WKWebView 中,前端 window 对象里的 alert()、confirm() 和 prompt() 被前端调用是不会直接显示的,需要在 WKUIDelegate 中进行重写。
例如:
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alert = UIAlertView.init(title: nil , message: message, delegate: nil, cancelButtonTitle: "确定")
alert.show()
completionHandler()
}