Alamofire HTTPS 证书信任

2016-12-12  本文已影响352人  拥抱月亮的大星星

使用版本

操作

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)
        }
        
    
    }


上一篇下一篇

猜你喜欢

热点阅读