tags:

views:

44

answers:

1

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

A: 

Bump.

Are you by any chance using the new 4.2 beta SDK? I am and am wondering if it is a problem there. I see no reason at all why canAuthenticateAgainstProtectionSpace is never called. I do get calls to connectionShouldUseCredentialStorage though.

greg
I am using 3.2 and 4.1 only - with the same behavior in both cases. I added connectionShouldUseCredentialStorage after reading your response and it does get called but has no effect on whether canAuthenticateAgainstProtectionSpace gets called. In other words, canAuthenticateAgainstProtectionSpace isn't called whether connectionShouldUseCredentialStorage returns YES or NO. Thanks for taking a look though.
Rick Tyler