Aira Samson
Aira Samson

Reputation: 243

Alamofire 5 Migration

I'm having problems of migrating this code to Alamofire 5:

init(plugins: [PluginType] = []) {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "test.com.ph": .pinCertificates(
            certificates: ServerTrustPolicy.certificates(),
            validateCertificateChain: true,
            validateHost: true
        )
    ]
    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 120 // default is 60.
    let manager = Manager(
        configuration: configuration,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )

    super.init(manager: manager, plugins: plugins)
}

I have modified my code same but I don't know how to initialize the session manager:

let pinEvaluator = PinnedCertificatesTrustEvaluator(certificates: Bundle.main.af.certificates,
                                                    acceptSelfSignedCertificates: true,
                                                    performDefaultValidation: true,
                                                    validateHost: true)

let trustManager = ServerTrustManager.init(evaluators: ["test.com.ph": pinEvaluator])

let sessionManager = Session.init(configuration: .default, serverTrustManager: trustManager)

super.init(session: sessionManager,plugins: plugins)

enter image description here

Can someone help me?

Upvotes: 1

Views: 1076

Answers (1)

Jon Shier
Jon Shier

Reputation: 12770

As the error states, you made a request to a host you hadn't registered with your ServerTrustEvaluating value. That is, a request that wasn't to test.com.ph. You'll want to ensure you're making requests to the proper host, or that you've used the correct host for your certificate pinning.

If you expect to make requests to multiple hosts, you can disable this behavior by initializing the ServerTrustManager with the allHostsMustBeEvaluated property set to false.

Upvotes: 2

Related Questions