使用WKWebView拦截请求

问题描述 投票:10回答:5

现在我正在使用UIWebViewcanInitWithRequest:NSURLProtocol我可以拦截所有请求并使用它我想要的。

在新的WKWebView这种方法没有,我没有找到类似的东西。

有人解决了这个问题吗?

ios objective-c
5个回答
3
投票

您可以通过为decidePolicyFor: navigationAction:实施WKNavigationDelegate方法拦截自iOS 8.0以来的WKWebView上的请求

 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 webView.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)

}

1
投票

在iOS 11中,WKWebView提出了名为WKURLSchemeHandler的自定义方案处理程序,您可以使用它来拦截自定义事件。有关更多信息,请查看此项目。 https://github.com/BKRApps/KRWebView


1
投票

有很多方法可以实现拦截器请求。

  1. 设置本地代理,使用WKNavigationDelegate -[ViewController webView:decidePolicyForNavigationAction:decisionHandler:] 加载新请求并转发到本地服务器,在本地服务器端,您可以执行某些操作(例如,缓存)。
  2. 私人api。使用WKBrowsingContextController和自定义URLProtocol Class cls = NSClassFromString(@"WKBrowsingContextController"); SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:"); if ([(id)cls respondsToSelector:sel]) { // 把 http 和 https 请求交给 NSURLProtocol 处理 [(id)cls performSelector:sel withObject:@"http"]; [(id)cls performSelector:sel withObject:@"https"]; }
  3. 使用KVO获取系统http / https处理程序。(ios 11,*) WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; URLSchemeHandler *schemeHandler = [[URLSchemeHandler alloc] init]; [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"test"]; NSMutableDictionary *handlers = [configuration valueForKey:@"_urlSchemeHandlers"]; handlers[@"http"] = schemeHandler; handlers[@"https"] = schemeHandler; 所有这三种方式你可能需要处理CORS&post机构被剥离,你覆盖更改浏览器的选项请求(Preflighted_requests),你可以自定义http头来替换http体,以便剥离post体。

0
投票

我盲目地猜测,因为我只有我的Windows电脑。通过阅读Apple Developer文档,我收集的信息可能会带来一些关于如何解决问题的想法。

基于WKWebView

将delegate属性设置为符合WKUIDelegate协议的对象,以跟踪Web内容的加载。

此外,我看到我们可以设置我们的navigationDelegate

weak var navigationDelegate: WKNavigationDelegate? { get set }

WKNavigationDelegate协议的方法可帮助您实现在Web视图接受,加载和完成导航请求的过程中触发的自定义行为。

然后,在我们创建并设置自定义WKNavigationDelegate之后,我们将覆盖一些方法来拦截我们可能正在寻找的东西。我找到了一些感兴趣的Responding to Server Actions部分,因为他们收到WKNavigation作为参数。此外,你可能想要浏览WKNavigationActionWKNavigationResponse,看看是否有可能帮助我们实现目标的东西。

顺便说一句,我只是提出了一些想法,以便我们可以解决这个问题,这些想法可能是100%错误,因为我自己没有尝试过。


0
投票

可以使用WKURLSchemeHandler拦截要在WKWebView中加载的每个请求,唯一的缺点是你不能注册http或https方案进行拦截,

解决方法是,将您的http / https url方案替换为自定义方案网址,例如xyz://,例如可以像xyz://google.com一样加载https://google.com现在你将在WKURLSchemeHandler中获得一个回调,你再次将它替换回https并以编程方式加载数据并调用urlSchemeTask.didReceive(response)

这样,每个https请求都将出现在您的处理程序中。

© www.soinside.com 2019 - 2024. All rights reserved.