Error Domain=NSURLErrorDomain Code=-1202,Https服务器证书无效

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

原因:安全证书是自建证书,没有得到认证。

解决方法:

1.导入NSURLSessionDelegate

2.访问网络时创建NSURLSession对象时,采用NSURLSession *session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];

3.在认证的代理方法中强制信任证书

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
    NSURLCredential *card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,card);
}

参考链接:http://www.qingpingshan.com/rjbc/ios/181533.html

原文地址:https://www.cnblogs.com/wobuyayi/p/6269688.html