我只是想忽略一个未签名的SSL证书,我是否直接按照我在网上找到的如何覆盖NSURLCOnnection的两个委托方法的示例,如下所示,以允许我的应用程序使用HTTPS连接到具有自我的服务器签名证书:
- (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];
}
尽管签名与NSURLConnection.h中的签名相同,并且下面的didFailWithError实现被调用,但上述两种方法永远不会被调用:
- (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;
}
当我使用同一个类访问非安全URL时,我的NSURLConnection委托方法的其余部分工作正常。
我完全不知道为什么这两个委托方法没有合作。
谢谢,
瑞克