与WKWebView的拦截请求 [英] Intercept request with WKWebView

查看:507
本文介绍了与WKWebView的拦截请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用UIWebView,并且使用NSURLProtocolcanInitWithRequest:,我可以拦截所有请求,并根据需要进行处理.

Now i'm using UIWebView and with canInitWithRequest: of NSURLProtocol i can intercept all requests and do with it what I want.

在新的WKWebView这种方法中,没有,而且我没有发现类似的东西.

In the new WKWebView this method there isn't, and i not found something similar.

有人解决了这个问题吗?

Has someone resolved this problem?

推荐答案

通过为WKNavigationDelegate

 func webView(_ webView: WKWebView, decidePolicyFor 
       navigationAction: WKNavigationAction, 
       decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {

    //link to intercept www.example.com

    // navigation types: linkActivated, formSubmitted, 
    //                   backForward, reload, formResubmitted, other

    if navigationAction.navigationType == .linkActivated {
        if navigationAction.request.url!.absoluteString == "http://www.example.com" {
            //do stuff

            //this tells the webview to cancel the request
            decisionHandler(.cancel)
            return
        }
    }

    //this tells the webview to allow the request
    decisionHandler(.allow)

}

这篇关于与WKWebView的拦截请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆