title: iOS: passing custom NSURL to NSURLProtocol
body: <p>I need to pass some extra informations along with <code>UIWebView loadRequest:</code> so that it reaches my implementation of <code>NSURLProtocol</code>. The information cannot be bound to <code>NSURLRequest</code> because the information must be retained with <code>NSURLRequest mainDocumentURL</code> as well. So i subclassed <code>NSURL</code> and constructed <code>NSURLRequest</code> with it. I already knew that the <code>NSURLRequest</code> which reaches <code>NSURLProtocol startLoading</code> is NOT the instance i have fed to <code>UIWebView loadRequest</code>, so i implemented <code>NSURL copyWithZone</code> too, naively expecting that URL loading system will use it.</p>
  
  <p>Now, <code>NSURLProtocol canInitWithRequest</code> is called not once as one would reasonably expect, but at least 4 times before <code>startLoading</code>. First 2 times of that, the incoming <code>NSURLRequest</code> still contains my custom <code>NSURL</code> implementation. Then an unfortunate internal code called <code>CFURLCopyAbsoluteURL</code> asks for the <code>absoluteURL</code> of my custom <code>NSURL</code> and the next <code>canInitWithRequest</code> (and subsequent <code>startLoading</code>) already gets a completely new <code>NSURLRequest</code> with fresh <code>NSURL</code> in it. <code>copyWithZone</code> is never called and my subclassed <code>NSURL</code> is lost.</p>
  
  <p>Before i give up and implement an inferior and fragile solution with attaching stuff directly to the URL string, i would like to ask the wizards of higher level, whether they see a way how to catch that initial blink on the <code>NSURLProtocol</code> radar or how to trick <code>CFURLCopyAbsoluteURL</code> into carrying my custom instance. I have tried to hack <code>NSURL absoluteURL</code> by returning again a new instance of my custom NSURL class, but it didn't help. I have seen some promise in <a href="https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSURLProtocol_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLProtocol/setProperty%3aforKey%3ainRequest%3a" rel="nofollow"><code>NSURLProtocol setProperty</code></a> functionality, but now it appears pretty useless. URL loading system creates new instances of everything happily and <code>NSURLRequest</code> arrived in <code>NSURLProtocol</code> seems to be the same as the one entered into <code>UIWebView</code> only accidentally.</p>
  
  <p><strong>UPDATE:</strong> ok i wanted to keep the post as short as possible, but the even the first reply is asking for technical background, so here we go: i've got multiple <code>UIWebView</code>s in app. These views may run requests concurrently and absolutely can run requests for the same URL. It's like tabs in desktop browser. But i need to distinguish which <code>UIWebView</code> was the origin of each particular <code>NSURLRequest</code> arriving to the <code>NSURLProtocol</code>. I need a context being carried with each URL request. I can't simply map the URLs to data, because multiple <code>UIWebViews</code> may be loading the same URL at any moment.</p>
  
  <p><strong>UPDATE 2:</strong> Attaching the context information to <code>NSURL</code> is preferred and, as far as my understanding goes, the only usable. The issue is that requests for resources referenced inside page (images etc.) do not go through <code>UIWebViewDelegate</code> at all and end up in <code>NSURLProtocol</code> directly. I don't have a chance to touch, inspect or modify such requests anywhere prior to <code>NSURLProtocol</code>. The only contextual link for such requests is their <code>NSURLRequest mainDocumentURL</code>.</p>