这不是一个重复的问题,请继续阅读。我正在将我的应用程序中已弃用的代码升级到 iOS10 合规性。
错误:NSURLSession 给我带来了麻烦,这个臭名昭著的错误,以及 9806 和 9801,取决于我放在 .plist 文件中的内容:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
我的代码:在我的 Info.plist 文件中,我有:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>https://my.own-server.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowInsecureHTTPSLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
</dict>
</dict>
在我的 ObjectiveC 代码中,我有这个:
NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session __unused = [NSURLSession sessionWithConfiguration:defaultConfiguration delegate:[PortalRequest alloc] delegateQueue:[NSOperationQueue mainQueue]]; operationQueue:[NSOperationQueue mainQueue]];
requestContainer.sessionDataTask = [session dataTaskWithRequest:request];
[self.sessionDataTask resume];
在调用 URLSession 的类的 DELEGATE 方法中,我可以看到 didReceiveChallenge:
LOG: ** NSURLSession IOS10 ** - didReceiveChallenge called
...最后我得到了错误:
[TIMESTAMP] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9806)
服务器 其他答案中提出的解决方案都不起作用,是因为 Apple 的 2017 年截止日期吗? https://techcrunch.com/2016/06/14/apple-will-require-https-connections-for-ios-apps-by-the-end-of-2016/ 根据这个在线安全分析工具,https: //www.ssllabs.com/ssltest/analyze.html 后端支持 TLS 1.2
关于如何解决这个问题的任何想法?你知道在哪里可以找到一些 iOS 示例代码来 100% 确定我指向的端点是无罪的吗?
更新此代码对我有用,对此有何意见?:
- (void)URLSession:(NSURLSession *)session
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler{
NSLog(@"*** KBRequest.NSURLSessionDelegate - didReceiveChallenge IOS10");
if([challenge.protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust])
{
if([challenge.protectionSpace.host
isEqualToString:@"engine.hello-indigo.com"])
{
NSURLCredential *credential =
[NSURLCredential credentialForTrust:
challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
else
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}