Alamofire HTTPS 证书信任

使用版本

  • Xcode 8.1
  • Swift 3.0
  • Alamofire 4.1.0

操作

1.http请求在plist里面的App Transport Security Settings->Allow Arbitrary Loads = YES 依旧打开
2.在APPDelegate假如一下方法(请求前添加,这里我放在APPDelegate)

/// 支持https
    static func validateHTTPS(){
     
        let manager = SessionManager.default
        manager.delegate.sessionDidReceiveChallenge = { session, challenge in
            var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
            var credential: URLCredential?
            
            if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
                disposition = URLSession.AuthChallengeDisposition.useCredential
                credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            } else {
                if challenge.previousFailureCount > 0 {
                    disposition = .cancelAuthenticationChallenge
                } else {
                    credential = manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
                    
                    if credential != nil {
                        disposition = .useCredential
                    }
                }
            }
            return (disposition, credential)
        }
        
    
    }


你可能感兴趣的:(Alamofire HTTPS 证书信任)