大家好,我们之前使用的是 alamofire 4.9.1,最近我升级到了 Alamofire 5,因此我们面临以下错误
此服务器的证书无效。您可能正在连接到伪装成“XXX.XXX.XX.X”的服务器,这可能会使您的机密信息处于危险之中。" UserInfo={NSLocalizedRecoverySuggestion=您仍然想连接到服务器吗?,
在 Alamofire 4.9.1 中,我们通过覆盖 didReceive 挑战方法解决了同样的问题,如下所示
delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .cancelAuthenticationChallenge
} else {
credential = WebAPIManager.manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
if credential != nil {
disposition = .useCredential
}
}
}
return (disposition, credential)
}