5

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?

KevinH
  • 2,034
  • 1
  • 12
  • 12
  • NSURL *url = [NSURL URLWithString:urlAddress]; //Create a URL object. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //URL Requst Object [web loadRequest:requestObj]; – Kamleshwar Oct 25 '12 at 04:59
  • If that NSURLRequest garners an HTML response that has, say, img tags in it, the UIWebView will automatically launch secondary requests to retrieve that data. I want to correlate those requests as well. – KevinH Oct 25 '12 at 05:41

2 Answers2

0

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

eulr
  • 171
  • 2
  • 19
  • If I have a webpage on www.yahoo.com that has an image link to maps.google.com, the UIWebView's `URL` property will have no correlation with the URL represented in the image request. – KevinH Apr 11 '13 at 21:39
0

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!

Lytic
  • 786
  • 5
  • 12