1

I'm using Alamofire to SSL Pinning. For this, I use the below code:

import Foundation
import Alamofire
import SwiftyJSON   

class CertificateManager {
    init() {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "test.example.com": .pinCertificates(
                certificates: ServerTrustPolicy.certificates(),
                validateCertificateChain: true,
                validateHost: true
            ),
            "insecure.expired-apis.com": .disableEvaluation
        ]

        let sessionManager = SessionManager(
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }
}

but these errors occurred:

Use of undeclared type error

My pod file is: pod 'Alamofire', '~> 5.0.0-rc.2'.

I searched and tested some solutions, but can't resolve errors. How can I fix this?

I tried this answer too. +

Zonily Jame
  • 5,053
  • 3
  • 30
  • 56
Hajitsu
  • 764
  • 17
  • 49
  • Did you do `import Alamofire`? – Sweeper Oct 13 '19 at 12:48
  • Yes I did, I import these `import Foundation import Alamofire import SwiftyJSON` – Hajitsu Oct 13 '19 at 12:49
  • Clean and rebuild project, did you try that? – Joakim Danielson Oct 13 '19 at 12:55
  • @JoakimDanielson Yes, I tried this. Clean, Build, Also wipe DeivenData. – Hajitsu Oct 13 '19 at 12:56
  • 2
    Alamofire v5 has new APIs but they haven't created a migration guide yet because it's still a release candidate. You can read more about it in [this ticket](https://github.com/Alamofire/Alamofire/issues/2755). See also [Kamran](https://stackoverflow.com/users/2395636/kamran)'s [answer](https://stackoverflow.com/a/58363900/5928180) below – Zonily Jame Oct 14 '19 at 00:04

2 Answers2

5

In Alamofire version you are using, ServerTrustPolicy and SessionManager no longer exist. You can try the newly introduced api's as below,

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

let trustManager = ServerTrustManager.init(evaluators:
    ["test.example.com": pinEvaluator,
     "insecure.expired-apis.com": disableEvaluator
    ])

let sessionManager = Session.init(configuration: .default, serverTrustManager: trustManager)
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • What is the use of pinEvaluator and disableEvaluator? – Aira Samson Apr 28 '20 at 11:20
  • @AiraSamson To encrypt your data for secure transmission you use certificate pinning(`pinEvaluator`). If some host is disabled for evaluation then you would use `DisabledEvaluator` – Kamran Apr 28 '20 at 11:38
4

As @Kamran post.

Alamofire 5.0, all api has big change

Jadian
  • 4,144
  • 2
  • 13
  • 10