开发过程中遇到WKWebView 打开某个链接的时候,链接会跳转到另一个链接,导致打开的速度变慢,参考这篇文章WKWebView通过decidePolicyForNavigationAction获取当前页面url的问题
1545204595661.jpg 1545204618888.jpg因为之前都是在didFinish 方法中去掉loading ,通过调试发现,跳转其他链接都可以很快打开,
后来在decidePolicyFor 方法中,当要跳转到 另一个连接的时候,直接去掉loading,页面很快就可以加载出来。
代码:
//判断链接是否可以跳转
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
printLog(message:navigationAction.navigationType)
printLog(message: navigationAction.request.url?.absoluteString)
if webUrl == "https://m.qlchat.com/wechat/page/live/2000002682331133" {
webview.evaluateJavaScript("document.location.href") { [weak self](data, error) in
if let str = data as? String,str == "https://m.qlchat.com/page/login?redirect_url=https%3A%2F%2Fm.qlchat.com%2Fwechat%2Fpage%2Flive%2F2000002682331133%3FloginType%3DqrCode" {
self?.loading?.remove()
self?.shareBtn.isHidden = false
}
}
decisionHandler(WKNavigationActionPolicy.allow)
}else{
decisionHandler(WKNavigationActionPolicy.allow)
}
return
}
网友评论