如何使用NSURLConnection sendAsynchronousRequest处理HTTPS请求

时间:2013-12-26 11:07:23

标签: ios objective-c objective-c-blocks

我的应用中有一个HTTPS请求,我这样做: -

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

对于身份验证,我已实现了以下方法

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
 {
   return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
 }

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
 {
   if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
      {
        if ([challenge.protectionSpace.host isEqualToString:[[NGHelper sharedInstance] getBaseURL]])
           {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
           }
       }

[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

但现在我想使用[NSURLConnection sendAsynchronousRequest:theRequest queue:bqueue completionHandler:^(NSURLResponse* response, NSData* data,NSError* error){}];

请指导我如何使用oprationqueue实现身份验证方法。

0 个答案:

没有答案