如何在Swift Alamofire中使用SSL证书?

DRP*_*DRP 5 ssl https ios swift alamofire

我正在使用HTTPS Web服务升级我的iOS应用程序。

我已经用SSL证书升级了Web服务器。但是从iOS代码方面不知道该怎么办?

我需要随同Web请求一起传递任何证书吗?

我正在使用Alamofire发出Web请求。

谢谢

Mat*_*att 3

简单的谷歌搜索会给你很多结果。

例如,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
  }
}
Run Code Online (Sandbox Code Playgroud)