I need to pass some extra informations along with UIWebView loadRequest:
so that it reaches my implementation of NSURLProtocol
. The information cannot be bound to NSURLRequest
because the information must be retained with NSURLRequest mainDocumentURL
as well. So i subclassed NSURL
and constructed NSURLRequest
with it. I already knew that the NSURLRequest
which reaches NSURLProtocol startLoading
is NOT the instance i have fed to UIWebView loadRequest
, so i implemented NSURL copyWithZone
too, naively expecting that URL loading system will use it.
Now, NSURLProtocol canInitWithRequest
is called not once as one would reasonably expect, but at least 4 times before startLoading
. First 2 times of that, the incoming NSURLRequest
still contains my custom NSURL
implementation. Then an unfortunate internal code called CFURLCopyAbsoluteURL
asks for the absoluteURL
of my custom NSURL
and the next canInitWithRequest
(and subsequent startLoading
) already gets a completely new NSURLRequest
with fresh NSURL
in it. copyWithZone
is never called and my subclassed NSURL
is lost.
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 NSURLProtocol
radar or how to trick CFURLCopyAbsoluteURL
into carrying my custom instance. I have tried to hack NSURL absoluteURL
by returning again a new instance of my custom NSURL class, but it didn't help. I have seen some promise in NSURLProtocol setProperty
functionality, but now it appears pretty useless. URL loading system creates new instances of everything happily and NSURLRequest
arrived in NSURLProtocol
seems to be the same as the one entered into UIWebView
only accidentally.
UPDATE: 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 UIWebView
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 UIWebView
was the origin of each particular NSURLRequest
arriving to the NSURLProtocol
. I need a context being carried with each URL request. I can't simply map the URLs to data, because multiple UIWebViews
may be loading the same URL at any moment.
UPDATE 2: Attaching the context information to NSURL
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 UIWebViewDelegate
at all and end up in NSURLProtocol
directly. I don't have a chance to touch, inspect or modify such requests anywhere prior to NSURLProtocol
. The only contextual link for such requests is their NSURLRequest mainDocumentURL
.
NSURL
than the URL. So the question is, what kind of information you're trying to pass through? Typically you would side-load this, by storing a map of URLs to "other data" in some object accessible to theNSURLProtocol
, but I think a better understanding of what you're trying to solve would help here. – Seamaid