繁体   English   中英

在全球范围内为NSURLSession重新编码NSRULProtocol

[英]Regestring NSRULProtocol for NSURLSessions Globally

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

  [NSURLProtocol registerClass:[CustomHTTPSProtocol class]] 

用于NSURLConnection。但是它不能与NSURLSession一起使用,因此我需要设置SessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]]; 问题是我使用此URLProtocol类仅处理身份验证挑战,并且在原始类的委托中获得了接收到的数据。

像这样

原始类

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类别

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

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

有人帮我。

当您自己实现提出请求(或像您一样重新发送请求)时,您的协议将成为质询发送者。 您负责创建NSURLAuthenticationChallenge对象,将自己设置为发送者,提供现有质询中的所有其他位,然后使用NSURLProtocolClient类进行发送。

查看Apple的CustomHTTPProtocol示例代码项目,以获取示例和更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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