Correlate NSURLRequest with UIWebView

ぃ、小莉子 提交于 2019-12-10 14:47:14

问题


In iOS, I'd like to be able to associate an NSURLRequest--any NSURLRequest, whether a navigation request or image or stylesheet request--with the UIWebView that sent it. This seems to call for the implementation of an NSURLProtocol subclass to capture and analyze requests as they come through. However, I can't find a way to correlate a request with its web view, and NSURLProtocol only seems to understand NSURLRequests.

This would be fine for navigation requests, as I could make the association in my UIWebViewDelegate. But this won't work for secondary resource requests such as images, which do not trigger UIWebViewDelegate methods.

Anyone have any thoughts as to the best way to make this association?


回答1:


If I understand your question correctly, you could just do NSURL *url = [[webView request] URL];




回答2:


In your custom protocol, canUseProtocol delegate method gets called for referenced resources as well. You may be able to leverage this hook to link your requests, but I am not sure if you can get a failure callback without cancelling that request and performing a separate one with the url.

Your question about referenced resources led me to that discovery, which allowed me to solve a caching problem i was having.

Thank you and I hope this helps in return!



来源:https://stackoverflow.com/questions/13061776/correlate-nsurlrequest-with-uiwebview

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