錯誤:“此服務器的證書無效。您可能正在連接到一個偽裝成“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); }
