3k

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

IOS HTTPS 忽略证书

Posted on 2012-06-26 14:57  3k  阅读(807)  评论(0)    收藏  举报
实现 NSURLConnectionDelegate 的代理方法: 

 

- (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];
}