Reputation: 372
I've been running through this tutorial from Ray Wenderlich around custom NSURLProtocols. I have a scenario where I'm intercepting particular NSURLRequests (call them 'request a') and firing off another request ('request b'). For the scenarios where this second request (b) fails, i want to be able to cancel the original request (a), at the moment it is just timing out after a period of time, but I want a bit more control and immediately fail it.
Within the custom NSURLProtocol I'm creating a new NSURLConnection for request b using the intercepted NSURLRequest (request a), but does anyone know if it's possible to get a reference to the original NSURLConnection which fired that request a? This would allow me to cancel it if the subsequent request b call failed?
Edit:
None of the NSURLConnection delegate methods will give me access to the originating NSURLConnection object, since self.connection in my example will relate to 'request b', not the originating request
See example delegate methods below:
- (void)startLoading {
self.connection = [NSURLConnection connectionWithRequest:self.request
delegate:self];
}
- (void)stopLoading {
[self.connection cancel];
self.connection = nil;
}
Upvotes: 0
Views: 97
Reputation: 10417
IIRC, you communicate with the original connection or session by calling methods in the NSURLProtocolClient
protocol on whatever object is in your NSURLProtocol
subclass's client
property. The object itself might be part of the NSURLConnection
or NSURLSession
machinery, depending on how the request was initiated, but either way, you call the same methods.
For example, you probably want to call this one:
- (void)URLProtocol:(NSURLProtocol *)protocol
didFailWithError:(NSError *)error;
See Apple's NSURLProtocolClient Protocol Reference for more details.
Upvotes: 0