Search code examples
swiftalamofirensurlsession

Alamofire - Use of undeclared type 'ServerTrustPolicy'?


The official Alamofire documentation on Github suggests using the following to disable evaluation of a url.

let serverTrustPolicies: [String: ServerTrustPolicy] = [
    "blahblah.com": .disableEvaluation
]

However, I'm getting the error:

Use of undeclared type 'ServerTrustPolicy'

(Yes, I have imported Alamofire)

Have Alamofire changed this?


Solution

  • Yes, it appears that Alamofire has changed this, so the documentation seems to be out of date:

    The previous ServerTrustPolicy enum has be refactored into classes which implement ServerTrustEvaluating

    Reference: https://github.com/Alamofire/Alamofire/pull/2344

    Your solution will be dependent on which version of Alamofire you are using. Certainly this would not impact you in versions 4.6.0 or below, so it seems you are using a newer version.

    In the latest master branch (representing 5.0.0-beta7 release), it seems the equivalent code you are looking for is the evaluators property on ServerTrustManager, and you can use the provided DefaultTrustEvaluator class for the ServerTrustEvaluating objects. There are several other evaluators in the same file if you require advanced usage.

    Related source files: https://github.com/Alamofire/Alamofire/blob/master/Source/ServerTrustEvaluation.swift