Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 486
Yoel Jimenez del valle Avatar asked Apr 05 '19 21:04

Yoel Jimenez del valle


People also ask

What is the use of Alamofire in Swift?

Alamofire is an HTTP networking library written in Swift. Alamofire helps to improve the quality of code. It is a simpler way to consume REST services. Alamofire is the basic tool for hundreds of projects.

What is Alamofire ios?

Alamofire is a Swift-based, HTTP networking library. It provides an elegant interface on top of Apple's Foundation networking stack that simplifies common networking tasks. Its features include chainable request/response methods, JSON and Codable decoding, authentication and more.


1 Answers

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)
}()
like image 91
Jon Shier Avatar answered Sep 24 '22 01:09

Jon Shier