问题描述
我们的应用程序中有很多Web视图,我最近添加了一个NSURLProtocol来拦截来自它们的一些请求。
Our app has a lot of web views in it and I recently added a NSURLProtocol to interceptor some of the requests from them.
我注意到了一些Web视图多次调用+ [NSURLPRotocol canInitWithRequest:]方法,看起来完全相同的请求。有时6或7次。我想弄清楚为什么会这样。
I've noticed that some of the web views are calling the +[NSURLPRotocol canInitWithRequest:] method multiple times with what appears to be exactly the same request. Sometimes 6 or 7 times. I'm trying to figure out why this might be occurring.
有没有人有这方面的经验?我已经注销了[NSURL absoluteString]和httpMethod值,它们对于每个请求都是相同的。我希望这个方法只能为服务器所需的任何给定文件或资源调用一次,而不是多次。它似乎每个网页都有所不同。
Does anyone have any experience with this? I've logged out the [NSURL absoluteString] and httpMethod values and they are the same for each request. I would expect that this method would only be called once for any given file or resource needed from a server, not multiple times. And it seems to vary per web page.
任何想法?
推荐答案
我不是专家,但AFAIK这是正常行为。对于同一请求,可以多次调用[NSURLPRotocol canInitWithRequest:]。如果您希望每个请求只收到一次通知,则应该在-startLoading方法中捕获它。我发现本教程有助于构建简单的NSURLProtocol子类,它将做到这一点:
I'm not expert, but AFAIK this is normal behavior. [NSURLPRotocol canInitWithRequest:] may be called multiple times for the same request. If you want to be notified just once per request, you should catch it in -startLoading method. I found this tutorial helpful to construct simple NSURLProtocol subclass which will do just that: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial
这篇关于NSURLProtocol canInitWithRequest:多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!