NSURLProtocol canInitWithRequest: called multiple times

懵懂的女人 提交于 2019-12-12 08:35:05

问题


Our app has a lot of web views in it and I recently added a NSURLProtocol to interceptor some of the requests from them.

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.

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.

any ideas?


回答1:


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



来源:https://stackoverflow.com/questions/23238106/nsurlprotocol-caninitwithrequest-called-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!