iOS9 更新 AFNetworking 调用以使用 HTTPS(错误 -1200、-9824)的正确方法是什么?

iOS9 What is the proper way to update AFNetworking calls to work with HTTPS (error -1200 , -9824)?

我正在从 iOS8 切换到 iOS9 并且 AFNetworking Web 服务通过 HTTPS 调用,之前可以正常工作。我得到 error -1200error -9824 这个问题与使用自签名证书有关。

Error Domain=kCFErrorDomainCFNetwork Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSErrorFailingURLStringKey=[server url], NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFNetworkCFStreamSSLErrorOriginalValue=-9824

我看到了这个相关问题:,答案如下,但是,答案很含糊,并没有真正告诉我如何解决这个问题。

ATS trusts only certificate signed by a well known CA, all others are rejected. As a consequence the only solution with a Self signed certificate is to set anexception with NSExceptionDomains.

更新 AFNetworking 调用以在 iOS9 上使用 HTTPS 的正确方法是什么?

过去我使用过许多变通方法,包括响应 NSURLAuthenticationChallenge 的方法,但我正在寻找更明确的方法。

iOS 9 强制使用 HTTPS 的连接为 TLS 1.2 以避免最近的漏洞。在 iOS 8 中甚至支持未加密的 HTTP 连接,因此旧版本的 TLS 也不会产生任何问题。作为解决方法,您可以将此代码片段添加到您的 Info.plist:

<key>NSAppTransportSecurity</key>  
  <dict>  
  <key>NSAllowsArbitraryLoads</key>  
  <true/>  
  </dict>  

因此您禁用了应用程序传输安全。希望对您有所帮助。

这是一个可能的解决方案,(但在上述情况下仍然不起作用)

允许无效的 SSL 证书

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production