可能重复:
Can I use NSURLCredentialStorage for HTTP Basic Authentication?
我如何接受自签名服务器证书?使用下面的代码,我只能在使用Safari接受服务器证书后进行连接/验证。
- (void)secure:(NSString *)username credentials:(NSString *)login
{
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:username
password:login
persistence:
NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:LIST_URL_HTTPS
port:443
protocol:@"https"
realm:@"Restricted Area"
authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
forProtectionSpace:space];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageURL]
cachePolicy:NSURLCacheStorageNotAllowed
timeoutInterval:30];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
}