开发者

Can't override NSURLConnection canAuthenticateAgainstProtectionSpace and didReceiveAuthenticationChallenge delegate methods

I am simply trying to ignore an unsigned SSL certificate and am I am directly following examples I found online of how to override two delegate methods of NSURLCOnnection as follows to allow my app to use an HTTPS connection to a server which has a self-signed certificate:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
 NSLog( @"canAuthenticateAgainstProtectionSpace() called" );

 return( [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] );
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
 NSLog( @"didReceiveAuthenticationChallenge() called" );

 NSArray *trustedHosts = [NSArray arrayWithObject:@"myhost.mydomain.com"];

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

The above 2 methods are NEVER called in spite of the fact that the signatures are identical to those in NSURLConnection.h and my implementation of didFailWithError, below, is called:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
 NSLog( @"didFailWithError()" );

 NSDictionary *userInfo = error.userInfo;

 NSArray *keys = userInfo.allKeys;
 NSArray *values = userInfo.allValues;

 for( int i = 0; i < keys.count; i++ )
 {
  NSLog( @"NSURLConnection.didFailWithError: %@: %@",
   (NSString *) [keys objectAtIndex:i], (NSString *) [values objectAtIndex:i] );
 } 

 badConnection = YES;

 asyncDone = YES;
}

The rest of my NSURLConnection delegate methods work perfectly when I use the same class to access a non secure URL.

I am utterly stumped as to why just these two delegate methods are not cooperating.

Thanks,

开发者_运维技巧Rick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜