美文网首页
Alamofire HTTPS 证书信任

Alamofire HTTPS 证书信任

作者: 拥抱月亮的大星星 | 来源:发表于2016-12-12 13:51 被阅读352次

    使用版本

    • 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 证书信任

          本文链接:https://www.haomeiwen.com/subject/pyummttx.html