iOS Https 服务器证书无效

来源:互联网 发布:手机实用小五金软件 编辑:程序博客网 时间:2024/05/18 13:43

错误:“此服务器的证书无效。您可能正在连接到一个伪装成“www.xxxxxx.com”的服务器,这会威胁到您的机密信息的安全。”

原因:安全证书是自己的服务器生成的,未获权威认证。

方案:需要iOS客户端忽略证书验证。关键代码:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{    return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];}- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{    if ([challenge.protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust])    {        [[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]forAuthenticationChallenge:challenge];        [[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];    }}

0 0
原创粉丝点击