NSURLProtocol in NSURLSession doesnt need to

Hi All,


I noticed that just using protocol classes in NSURLSession without registering it also works. So do we no longer need to register the protocol class with the url loading system?


The below code works:

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        sessionConfiguration.protocolClasses = @[[DemoNSURLProtocol class]];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];


   [[session dataTaskWithURL:[NSURL URLWithString:@"http://www.apple.com/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"test");
    }] resume];


@implementation DemoNSURLProtocol
+(BOOL)canInitWithRequest:(NSURLRequest *)request{
    NSLog(@"%@",request.URL.absoluteString);
    return [@[ @"http", @"https" ] containsObject:request.URL.scheme];
}
+(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request{
    return request;
}
-(void)startLoading{
}
-(void)stopLoading{
}


so do we no longer need to

[NSURLProtocol registerClass:[DemoNSURLProtocol class]]

Replies

The registration makes it available to NSURLConnection and the shared NSURLSession. If you don’t need that, it’s fine to skip the registration and just apply it to your session via `protocolClasses`. Share and Enjoy — Quinn “The Eskimo!” Apple Developer Relations, Developer Technical Support, Core OS/Hardware `let myEmail = "eskimo" + "1" + "@apple.com"`

One thing that is not clear to me is, and I would really appreciate some help here:


1. If I register a CustomNSURLProtocol using:

[NSURLProtocol registerClass:CustomNSURLProtocol]


2. And if I start a new NSURLSession based on the defaultConfiguration like:


NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];


Those requests for that specific session will be intercepted by my CustomNSURLProtocol.

That's ok, make sense.


But what If I switch the order of the previous steps:

1. First create a NSURLSession based on the defaultSessionConfiguration and after that..

2. attempt to register my CustomNSURLProtocolWill my requests be intercepted too?


Do the defaultSessionConfiguration have that protocol registered?


And how does registerClass: relates with the:


sessionConfiguration.protocolClasses

Is that even related?

OK, I have to ask, why does this matter?

I agree that the semantics in this space are not as well documented as they could be but the recommended steps are very clear:

  • If you want your protocol to be used by NSURLConnection and the shared NSURLSession, register it.

  • If you want your protocol to be used by a specific NSURLSession, add it to

    protocolClasses
    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"