用于NSURLSessions的Regestring NSRULProtocol全球范围内

时间:2015-08-26 11:43:49

标签: ios xcode nsurlsession nsurlprotocol

我正在尝试使用NSURLProtocol来处理每个NSURLSession连接的身份验证质询。我用了

  [NSURLProtocol registerClass:[CustomHTTPSProtocol class]] 

对于NSURLConnection。但它不能与NSURLSession一起使用,所以我需要设置SessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]];。 问题是我使用这个URLProtocol类只处理身份验证挑战,我在原始类的委托中收到数据。

像这样的东西

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);
   }
 }

使用上述逻辑,我能够全局处理所有请求的身份验证并单独处理响应。但是使用NSURLSession,如果我使用NSURLProtocol,身份验证在Protocol类中完成,但我无法接收数据,因为我的原始类委托没有被调用。

有人帮助我。

1 个答案:

答案 0 :(得分:0)

当您自己实施请求(或重新发送)时,您的协议将成为挑战发件人。您有责任创建 NSURLAuthenticationChallenge 对象,将自己设置为发件人,提供现有质询中的所有其他位,并使用 NSURLProtocolClient 类发送。

查看Apple的CustomHTTPProtocol Sample Code项目以获取示例和更多信息。