swift 忽略https证书

452 阅读1分钟

1.不在代理中实现

let sessionManager = SessionManager.default
sessionManager.delegate.sessionDidReceiveChallenge = {  session,challenge in
    return (URLSession.AuthChallengeDisposition.useCredential,URLCredential(trust:challenge.protectionSpace.serverTrust!))
}

2. 在代理中实现

// URLSessionTaskDelegate
    func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
            let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            completionHandler(.useCredential,credential)
        }
        completionHandler(.performDefaultHandling,nil)
    }