How to use Alamofires ServerTrustPolicy.disableEvaluation in swift 5 alamofire 5.0.3

天涯浪子 提交于 2021-01-21 12:17:05

问题


in alamofire 4 I used this code to disable sever evaluation:

private var Manager : Alamofire.Session = {
        // Create the server trust policies
        let serverTrustPolicies: [String: ServerTrustPolicy] = ["serverurl.com": .disableEvaluation]
        // Create custom manager
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.Session.defaultHTTPHeaders
        let man = Alamofire.Session(
            configuration: URLSessionConfiguration.default,
            serverTrustPolicyManager: ServerTrustManager(policies: serverTrustPolicies)
        )
        return man
    }()

but is not working anymore in alamofire 5 with swift 5 xcode 10.2, I got this errors.

Use of undeclared type 'ServerTrustPolicy' Type 'Session' has no member 'defaultHTTPHeaders'

but I cant find a new way to make this work with alamofire 5.


回答1:


ServerTrustPolicy has been replaced with the protocol ServerTrustEvaluating in Alamofire 5, and DisabledEvaluator has replaced the .disabled enum case. To replicate the custom setup you had before:

private let session: Session = {
    let manager = ServerTrustManager(evaluators: ["serverurl.com": DisabledEvaluator()])
    let configuration = URLSessionConfiguration.af.default

    return Session(configuration: configuration, serverTrustManager: manager)
}()


来源:https://stackoverflow.com/questions/55543462/how-to-use-alamofires-servertrustpolicy-disableevaluation-in-swift-5-alamofire-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!