According to the documentation of NSURLSessionConfiguration::protocolClasses, there is no guaranty, that my custom url protocol will be used. How can I ensure, that it is used whenever I set it to protocolClasses property?
Prior to handling a request, an NSURLSession object searches the default protocols first and then checks your custom protocols until it finds one capable of handling the specified request. It uses the protocol whose canInitWithRequest: class method returns YES, indicating that the class is capable of handling the specified request.
I can't set an array with single URL protocol, because it has logic for canInitWithRequest: method and might not handle all request.
NSArray *currentProtocolClasses = sessionConfiguration.protocolClasses ?: @[]; NSMutableArray *protocolClasses = [NSMutableArray arrayWithArray:currentProtocolClasses]; [protocolClasses insertObject:[CustomURLProtocol class] atIndex:0]; sessionConfiguration.protocolClasses = protocolClasses;