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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM