美文网首页
关于WebView(WebKit)

关于WebView(WebKit)

作者: Coder_Star | 来源:发表于2019-08-07 16:56 被阅读0次

1、代理

WKWebview代理主要是WKUIDelegate以及WKNavigationDelegate两部分,

  1. WKNavigationDelegate
    可以分为页面跳转以及页面渲染两部分
//页面调转部分
//发送请求前执行,决定是否跳转
optional public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
//用户认证
optional public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
//收到回应,决定是否跳转
optional public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)
//后台重定向
optional public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!)
//失败
optional public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)

//页面渲染
//网页开始加载时调用 
optional public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
//网页正在加载
optional public func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!)
//网页加载完成
optional public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
//网页加载失败
optional public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)

//关闭
optional public func webViewWebContentProcessDidTerminate(_ webView: WKWebView)
  • js注入相关
//注入宽度自适应标签
 let js = "var oMeta = document.createElement('meta');oMeta.content = 'initial-scale=0.6,minimum-scale=0.5';oMeta.name = 'viewport';document.getElementsByTagName('head')[0].appendChild(oMeta);"
webView.evaluateJavaScript(js, completionHandler: nil)
//body滚动到顶部
webView.evaluateJavaScript("document.body.scrollTop = document.documentElement.scrollTop = 0;", completionHandler: nil)
//禁止长按出现菜单
webView.evaluateJavaScript("document.documentElement.style.webkitUserSelect='none';", completionHandler: nil)       
webView.evaluateJavaScript("document.documentElement.style.webkitTouchCallout='none';", completionHandler: nil)
//放大文字
webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '266%'",completionHandler: nil)

2、问题集锦

  • js的alert标签在ios的webview中无法被触发显示出来,需要使用代码手动去监听然后转换为ios拥有的UIAlertController。监听代码如下
//提示框
 public func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        let alertController = UIAlertController(title: "提示",message: message, preferredStyle: .alert)
        let okAciton = UIAlertAction(title:"确定",style:.default,handler: {action in
            completionHandler()
        })
        alertController.addAction(okAciton)
        present(alertController, animated: true, completion: nil)
}
//确认框
public func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
        let alertController = UIAlertController(title: "提示",message: message, preferredStyle: .alert)
        let okAciton = UIAlertAction(title:"确定",style:.default,handler: {action in
            completionHandler(true)
        })
        let cancelAciton = UIAlertAction(title:"取消",style:.cancel,handler: {action in
            completionHandler(false)
        })
        alertController.addAction(okAciton)
        alertController.addAction(cancelAciton)
        present(alertController, animated: true, completion: nil)
 }
//输入框
public func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
        let alertController = UIAlertController(title:prompt,message: "", preferredStyle: .alert)
        alertController.addTextField {(textField: UITextField!) -> Void in
            textField.clearButtonMode = .whileEditing
            textField.text = defaultText
        }
        let okAciton = UIAlertAction(title:"完成",style:.default,handler: {action in
            if alertController.textFields != nil,alertController.textFields!.count > 0 {
                completionHandler(alertController.textFields![0].text)
            }else{
                completionHandler("")
            }
        })
        alertController.addAction(okAciton)
        present(alertController, animated: true, completion: nil)
 }

相关文章

网友评论

      本文标题:关于WebView(WebKit)

      本文链接:https://www.haomeiwen.com/subject/wmzitctx.html