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]]