学习笔记:WKWebView之WKUIDelegate(Swift)

此代理下有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)")
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值