简体   繁体   中英

Regestring NSRULProtocol for NSURLSessions Globally

I am trying to use NSURLProtocol to handle authentication challenges for every NSURLSession connection . I used

  [NSURLProtocol registerClass:[CustomHTTPSProtocol class]] 

for NSURLConnection .But it is not working with NSURLSession so i need to set SessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]]; . The problem is i use this URLProtocol class to only handle authentication challenges and i get received data's in the delegates of original class.

Something like this

OriginalClass

NSURLConnection

 -(void)sendURL
 {
   [NSURLProtocol registerClass:[CustomHTTPSProtocol class]];  //done globally in didFinishLaunching
    self.URL =[NSURL URLWithString:[[NSString stringWithFormat:@"https://www.google.co.in"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:self.URL];
 self.connection=[NSURLConnection connectionWithRequest:request delegate:self];
  }

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
   NSlog(@"received data...%@",data);
  }

NSURLSession

-(void)sendURL
 {
    self.URL =[NSURL URLWithString:[[NSString stringWithFormat:@"https://www.google.co.in"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
  NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:self.URL];
  sessionConfiguration =[NSURLSessionConfiguration defaultSessionConfiguration];
  sessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]];
 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
   NSURLSessionDataTask*task=  [session dataTaskWithRequest:checkInInfoRequest];
    [task resume];
   }

   - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
  {
 NSLog(@"data ...%@  ",data); //handle data here
  }

NSURLProtocol Class

NSURLConnection

-(void)startLoading
 {
   NSMutableURLRequest *newRequest = [self.request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:CustomHTTPSProtocolHandledKey inRequest:newRequest];

  self.connection=[NSURLConnection connectionWithRequest:newRequest delegate:self];

 }

 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

}
else
{
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

}

NSURLSession

 - (void) startLoading {

NSMutableURLRequest *newRequest = [self.request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:CustomHTTPSProtocolHandledKey inRequest:newRequest];

NSURLSession*session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
self.sessionTask=[session dataTaskWithRequest:newRequest];
[self.sessionTask resume];
 }

 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{

if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){

    NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
   }
 }

Using the above logic i was able to handle authentication for all request globally and handling response individually. But with NSURLSession if i use NSURLProtocol, authentication is done in Protocol class but i am not able to receive data, since my original class delegates are not called.

Somebody help me out.

When you implement making the request yourself (or resend it as you did), your protocol becomes the challenge sender. You are responsible for creating an NSURLAuthenticationChallenge object, setting yourself as the sender, providing all the other bits from the existing challenge, and sending it using the NSURLProtocolClient class.

Take a look at Apple's CustomHTTPProtocol Sample Code project for examples and further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM