【发布时间】:2015-06-18 09:13:47
【问题描述】:
我刚刚将我的代码更新到 Swift 2.0 以使用 Xcode 7。我的应用程序执行 NSURLAuthenticationMethodServerTrust 和 NSURLAuthenticationMethodClientCertificate 身份验证。
问题是NSURLAuthenticationMethodServerTrust 身份验证在我的模拟器上停止工作 - 但在我的 iOS 8.3 测试设备上仍然有效。除了我不是 Swift 2.0 的旧项目外,它还在工作。
错误:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
从 NSURLSession 检索到的错误:
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7fcf75053070 {NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7fcf73700d00>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorCodeKey=-9802, NSUnderlyingError=0x7fcf735284b0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)", NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://mywebapi/dosomething, NSErrorFailingURLStringKey=https://mywebapi/dosomething, _kCFStreamErrorDomainKey=3} [GetOneTimeTokenController.swift:76]
我仍然以 iOS 8.0 为目标进行部署。
这是我处理身份验证挑战的方式(使用自签名证书):
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate {
let urlCredential:NSURLCredential = NSURLCredential(
identity: identityAndTrust.identityRef,
certificates: identityAndTrust.certArray as [AnyObject],
persistence: NSURLCredentialPersistence.ForSession);
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, urlCredential);
} else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(trust: challenge.protectionSpace.serverTrust!));
} else {
challenge.sender?.continueWithoutCredentialForAuthenticationChallenge(challenge)
Logger.sharedInstance.logMessage("Unexpected Authentication Challange", .Error);
}
【问题讨论】: