iPhone HTTPS 訪問非信任站點(使用非信任證書,untrusted certificate)
阿新 • • 發佈:2019-02-20
-(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.