0

NSURLProtocol を使用して、すべての NSURLSession 接続の認証チャレンジを処理しようとしています。使った

  [NSURLProtocol registerClass:[CustomHTTPSProtocol class]] 

NSURLConnection の場合。ただし、NSURLSession では機能しないため、設定する必要がありますSessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]];。問題は、この URLProtocol クラスを使用して認証チャレンジのみを処理し、元のクラスのデリゲートで受信データを取得することです。

このようなもの

オリジナルクラス

NSURL接続

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

NSURLセッション

-(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 クラス

NSURL接続

-(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];
}

}

NSURLセッション

 - (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 クラスで行われますが、元のクラスのデリゲートが呼び出されないため、データを受信できません。

誰か助けて。

4

1 に答える 1

0

リクエストを自分で作成する (または行ったように再送信する) 場合、プロトコルがチャレンジ送信者になります。NSURLAuthenticationChallengeオブジェクトを作成し、自分自身を送信者として設定し、既存のチャレンジから他のすべてのビットを提供し、NSURLProtocolClientクラスを使用して送信する必要があります。

例と詳細については、Apple のCustomHTTPProtocol サンプル コードプロジェクトを参照してください。

于 2015-10-28T20:11:01.513 に答える