System.Net.WebException: 发生 SSL 错误,无法与服务器建立安全连接

System.Net.WebException: An SSL error has occurred and a secure connection to the server cannot be made

已找到相同的线程 ,但这并没有解决我的问题。

我在info.plist中添加了NSAppTransportSecurityNSAllowsArbitraryLoads

截图:

添加了 this 文章中的以下代码。

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>pm-admin.smartwcm.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowInsecureHTTPSLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
                <false/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSRequiresCertificateTransparency</key>
                <false/>
            </dict>
        </dict>
    </dict>

我正在使用 HTTP REST API。 当 运行 项目出现以下异常时:

System.Net.WebException: An SSL error has occurred and a secure connection to the server cannot be made. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?

我是不是遗漏了什么或做错了什么?

因为你必须使用证书。

class ViewController: UIViewController, URLSessionDelegate,URLSessionTaskDelegate {

var urlSession: Foundation.URLSession!

  override func viewDidLoad() {
        super.viewDidLoad()
        urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
}

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

        let serverTrust = challenge.protectionSpace.serverTrust
        let certificate = SecTrustGetCertificateAtIndex(serverTrust!, 0)
        let policies = NSMutableArray();
        policies.add(SecPolicyCreateSSL(true, (challenge.protectionSpace.host as CFString)))
        SecTrustSetPolicies(serverTrust!, policies);
        var result: SecTrustResultType = SecTrustResultType(rawValue: 0)!
        SecTrustEvaluate(serverTrust!, &result)
        let isServerTrusted:Bool = (result == SecTrustResultType.unspecified || result == SecTrustResultType.proceed)
        let remoteCertificateData:NSData = SecCertificateCopyData(certificate!)
        let pathToCert = Bundle.main.path(forResource: "certificateName", ofType: "crt")
        let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)!
        let credential:URLCredential = URLCredential(trust: serverTrust!)
        completionHandler(.useCredential, credential)

    }


}

原因: 自 iOS 9 起,iOS 将仅允许您的应用程序与默认实施最佳安全实践的服务器进行通信。必须在 Info.plist 中设置值才能与不安全的通信 servers.It 似乎你只 AllowInsecureHTTPSLoads 但忘记添加 AllowsInsecureHTTPLoads

解决方案:在您的info.plist中添加以下代码以信任您的域。

<key>NSAppTransportSecurity</key>
 <dict>
 <key>NSExceptionDomains</key>
 <dict>
  <key>pm-admin.smartwcm.com</key>
  <dict>       
   <key>NSExceptionRequiresForwardSecrecy</key>
   <false/>
   <key>NSExceptionAllowsInsecureHTTPLoads</key>
   <true/>
   <key>NSIncludesSubdomains</key>
   <true/>
   ...... 
  </dict>
 </dict>

这里是,你可以参考。