0

大家好,我们之前使用的是 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)
           }
4

1 回答 1

1

我也有同样的情况,正在努力重构适应新版本。到目前为止,我在 swift 5 中找到了这段代码:而且我还没有确定我可以从哪里获得“URLSessionTask”和“URLAuthenticationChallenge”。 代码 :

让委托 = SessionDelegate()

            let delegateQueue:OperationQueue = .init()
    
            delegateQueue.underlyingQueue = .global(qos: .background)
    
            let session = URLSession(configuration: URLSessionConfiguration.af.default, delegate: delegate, delegateQueue: delegateQueue)
    
            let manager = Alamofire.Session(session: session, delegate: SessionDelegate(), rootQueue: .global(qos: .background))
    
    
    
    manager.delegate.urlSession(session, task: <#T##URLSessionTask#>, didReceive: <#T##URLAuthenticationChallenge#>) { (<#URLSession.AuthChallengeDisposition#>, <#URLCredential?#>) in
        <#code#>
    }
于 2020-10-29T17:30:45.490 回答