My app uses subclassing of NSURLProtocol. There are several UIWebViews in the app and for specific algorithm implemented in NSURLProtocol I need to know which one of the UIWebViews sends the request.
My understanding is that object returned by [self client] should be somewhat connected with requesting object. But neither NSURLProtocolClient (that is the protocol implemented by object returned by [self client]) nor underlying object _NSCFURLProtocolBridge have any properties/methods to get the sender of the request.
Can anyone help me with ideas?
See https://stackoverflow.com/a/19700910/502149. To summarize, you can set the user agent for each
UIWebView
before you create it using the user defaults. Because the view doesn't take subsequent changes of the user agent into account, you'll end up with a different user agent in eachUIWebView
. You can use this inNSURLProtcol
to identify the view, and you can pass on the real UA agent so the server won't see any difference.Note that for the
UIWebView
to "remember" the UA string setting, it has to make at least one request before the setting is changed.in loadRequest:
in protocol:
Ideally, you shouldn't have to reference the UIWebView directly from the NSURLProtocol. However, I have had need in the past to have an NSURLProtocol send messages to UIWebView that are outside of the regular delegate messages... use NSNotificationCenter, I post notifications using the NSURLRequest object as the object, and subscribe to those notifications on my interested listener.
NSURLRequest
has a method calledmainDocumentURL
which returns the URL of the root document. You can possibly save that away in theUIWebViewDelegate
method like this,You can then look at the
mainDocumentURL
in yourNSURLProtocol
methods to identify theUIWebView
. This is not fool proof since it doesn't account for cases where multipleUIWebViews
load the same URL. But this is the best solution I could think of.Request is mutable type. So you can use
+[NSURLProtocol setProperty:forKey:inRequest:]
to set custom property. And+[NSURLProtocol propertyForKey:inRequest]
from inside ofNSURLProtocol