此代理下有8个方法,但其中只有4个是在iOS 8可用,1个在iOS 9添加,剩下3个都是10才可以用,也比较好理解,只做简单的代码展示了。
可在8下使用的四个方法
1、创建一个新的webview时调用:
optionalpublicfuncwebView(_webView:WKWebView, createWebViewWith configuration:WKWebViewConfiguration, for navigationAction:WKNavigationAction, windowFeatures:WKWindowFeatures) -> WKWebView?
2、分别在web产生了警告框、确认框、输入框时调用:
optionalpublicfuncwebView(_webView:WKWebView, runJavaScriptAlertPanelWithMessage message:String, initiatedByFrame frame:WKFrameInfo, completionHandler: @escaping () -> Swift.Void)
optionalpublicfuncwebView(_webView:WKWebView, runJavaScriptConfirmPanelWithMessage message:String, initiatedByFrame frame:WKFrameInfo, completionHandler: @escaping (Bool) -> Swift.Void)
optionalpublicfuncwebView(_webView:WKWebView, runJavaScriptTextInputPanelWithPrompt prompt:String, defaultText:String?, initiatedByFrame frame:WKFrameInfo, completionHandler:@escaping (String?) -> Swift.Void)
3、在webview关闭时调用
optional public func webViewDidClose(_ webView: WKWebView)
此关闭操作是有web发起(window.close),而不是由APP本身发起(dismiss或pop)
//MARK: - WKUIDelegate
/**
Description
- parameter webView: webView description
- parameter message: message description
- parameter frame: frame description
- parameter completionHandler: completionHandler必须执行
*/
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
debugPrint("confirm message : \(message)")
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
completionHandler(true)
}
/**
<#Description#>
- parameter webView: webView description
- parameter message: message description
- parameter frame: frame description
- parameter completionHandler: completionHandler必须执行
*/
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
debugPrint("webView run alert JS: \(message) ")
completionHandler()
}
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
debugPrint("webView run prompt JS: \(prompt) defaultText:\(defaultText)")
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
completionHandler("testText")
}
/**
创建一个新的 webView
- parameter webView: webView description
- parameter configuration: configuration description
- parameter navigationAction: navigationAction description
- parameter windowFeatures: windowFeatures description
- returns: return value description
*/
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
return webView
}
/**
9.0 later
- parameter webView: webView description
*/
@available(iOS 9.0, *)
func webViewDidClose(_ webView: WKWebView) {
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
}
/*
以下方法均为10.0之后
*/
@available(iOS 10.0, *)
func webView(_ webView: WKWebView, shouldPreviewElement elementInfo: WKPreviewElementInfo) -> Bool {
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
return true
}
@available(iOS 10.0, *)
func webView(_ webView: WKWebView, previewingViewControllerForElement elementInfo: WKPreviewElementInfo, defaultActions previewActions: [WKPreviewActionItem]) -> UIViewController? {
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
return self
}
@available(iOS 10.0, *)
func webView(_ webView: WKWebView, commitPreviewingViewController previewingViewController: UIViewController) {
debugPrint("class: \(object_getClassName(self))" + #function + "LINE: \(#line)")
}