Search code examples
iosswiftsslhttpsalamofire

How to use ssl certificate with Swift Alamofire?


I am upgrading my iOS apps with HTTPS web services.

I have upgrade my web server with SSL certificate. But don't know what to do from iOS code side?

Do I need to pass any certificate along with the web request?

I am using Alamofire for making web request.

Thanks


Solution

  • Simple googling would have given you so many results.

    For example, https://infinum.co/the-capsized-eight/how-to-make-your-ios-apps-more-secure-with-ssl-pinning

    func configureAlamoFireSSLPinning {
         let pathToCert = NSBundle.mainBundle().pathForResource(githubCert, ofType: "cer")
         let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)!
    
         self.serverTrustPolicy = ServerTrustPolicy.PinCertificates(
                certificates: [SecCertificateCreateWithData(nil, localCertificate)!],
             validateCertificateChain: true,
             validateHost: true
         )
    
         self.serverTrustPolicies = [
             "your-api.com": self.serverTrustPolicy!
         ]
    
         self.afManager = Manager(
             configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
             serverTrustPolicyManager: ServerTrustPolicyManager(policies: self.serverTrustPolicies)
         )
     }
    
    func alamoFireRequestHandler {
         self.afManager.request(.GET, self.urlTextField.text!)
             .response { request, response, data, error in
          // response management code
      }
    }