I try to add a custom NSURLProtocol
to a NSURLSession
in order to serve test data for some calls, while providing the normal internet connectivity for all other calls. I need to do this by adding the NSURLProtocol
to the session configuration's protocolClasses
, as the session resides in a library that is included in the project.
For some reason my setup does deliver the test data right when my custom protocol takes the request by returning true
for the canInitWithRequest
call, but for all other requests (when it returns false
) the default protocols that are still in the protocolClasses
behind my custom protocol get the request and even return true
on the canInitWithRequest
call, but they never send the request to the internet and deliver a request timeout after 30 seconds instead. When I remove my custom protocol from the protocolClasses
, everything works fine - just my test data is not served anymore, of course.
This is the code I use for installing the protocol:
NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSMutableArray* protocols = [NSMutableArray arrayWithArray:sessionConfiguration.protocolClasses];
[protocols insertObject:[MyProtocol class] atIndex:0];
sessionConfiguration.protocolClasses = protocols;
mySession = [NSURLSession sessionWithConfiguration:sessionConfiguration];
When I register the custom protocol with [NSURLProtocol registerClass:[MyProtocol class]]
, the fallthrough does work, but as written I can't use this approach in my project.
Any idea on why this is not working correctly?