根据项目需求,需要进行HTTPS网络请求并添加证书校验过程,由于没有做过,前前后后研究了两个多星期,才通过校验,可以正常进行网络请求,不多说了直接上代码
- 设置程序中的info.plist
(由于我们使用的是自签名的证书,而苹果ATS(App Transport Security)只信任知名CA颁发的证书,所以在iOS9下即使是HTTPS请求还是会被ATS拦截。)
(验证该域名是否通过ats认证 网上有很多,请自行搜索)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 开始正式验证
//MARK: https证书校验
func setAlamofireHttps() {
shareSessionManager.delegate.sessionDidReceiveChallenge = {(session: URLSession, challenge: URLAuthenticationChallenge) in
let method = challenge.protectionSpace.authenticationMethod
if method == NSURLAuthenticationMethodServerTrust {
LJQPrint("服务端证书认证")
return self.trustServerWithCer(challenge: challenge)
}else if method == NSURLAuthenticationMethodClientCer