1. 程式人生 > >iPhone HTTPS 訪問非信任站點(使用非信任證書,untrusted certificate)

iPhone HTTPS 訪問非信任站點(使用非信任證書,untrusted certificate)

-(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([trustedHosts containsObject:challenge.protectionSpace.host])[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace
.serverTrust] forAuthenticationChallenge:challenge];[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];}

Note that connection:didReceiveAuthenticationChallenge: can send its message to challenge.sender (much) later, after presenting a dialog box to the user if necessary, etc.