переопределить метод sessionDidReceiveChallenge, чтобы обойти проблему доверия к серверу в Alamofire5
Привет всем, мы использовали 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)
}
0 ответов
У меня такая же ситуация, и я пытаюсь провести рефакторинг и адаптироваться к новой версии. Пока я нахожу этот код в 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#>
}