Monday, August 8, 2022
HomeiOS Developmentios - Alamofire 5 SSL certificates pinning reveals AFError.sessionDeinitialized

ios – Alamofire 5 SSL certificates pinning reveals AFError.sessionDeinitialized


I’m utilizing ‘Alamofire’, ‘~> 5.4’ for request & response. I would like so as to add SSL certificates pinning. Nevertheless it reveals bellow error..

Alamofire.AFError.sessionDeinitialized

Right here is my Code :

       static func callingAp(){
        
        let cirfitcate = pinnedCertificates()
        let evaluators: [String: ServerTrustEvaluating] = [
            "mydomain.com": PinnedCertificatesTrustEvaluator(certificates: cirfitcate),
        ]
        let serverTrustManager = ServerTrustManager(evaluators: evaluators)
        let session = Session(serverTrustManager: serverTrustManager)
        let parameters: [String: Any] = [:]
        let headers: HTTPHeaders = [:]
        
        session.request("myapi", technique: .put up, parameters:parameters,headers: headers).responseJSON { response in
            if let knowledge = response.knowledge {
                do{
                    let loginResponse = strive JSONDecoder().decode(LoginResponseModel.self, from: knowledge)
                    
                }catch let err{
                    print("err 01 -->",err.localizedDescription)
                }
            }else{
                print("err 02-->",response)
            }
        }
        
    }
    
    static func pinnedCertificates() -> [SecCertificate] {
        var certificates: [SecCertificate] = []
        if let pinnedCertificateURL = Bundle.fundamental.url(forResource: "digicert_high_assurance_ev_root_ca", withExtension: "cer") {
            do {
                let pinnedCertificateData: CFData = strive Information(contentsOf: pinnedCertificateURL) as CFData
                if let pinnedCertificate: SecCertificate = SecCertificateCreateWithData(nil, pinnedCertificateData) {
                    certificates.append(pinnedCertificate)
                }
            } catch {
                print("certicate exception--")
            }
        }
        return certificates
    }

With out certificates pinning, it’s working wonderful.
What’s improper with my code? Please assist me…

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular