1 Reply
      Latest reply: Nov 4, 2016 1:51 AM by eskimo RSS
      swiftBank Level 1 Level 1 (0 points)

        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) {
            }] resume];


        @implementation DemoNSURLProtocol
        +(BOOL)canInitWithRequest:(NSURLRequest *)request{
            return [@[ @"http", @"https" ] containsObject:request.URL.scheme];
        +(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request{
            return request;


        so do we no longer need to

        [NSURLProtocol registerClass:[DemoNSURLProtocol class]]
        • Re: NSURLProtocol in NSURLSession doesnt need to
          eskimo Apple Staff Apple Staff (5,995 points)

          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"`