Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 803
DRP Avatar asked Oct 17 '22 19:10

DRP


1 Answers

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
  }
}
like image 88
Matt Avatar answered Nov 15 '22 05:11

Matt