swift5下使用Alamofire时如何无条件信任证书

How to trust certificates unconditionally when using Alamofire under swift5

谁能告诉我在 swift5 下使用 Alamofire 时如何无条件信任证书,谢谢。

以下是我的回答,希望对以后遇到同样问题的小伙伴有所帮助

extension ViewController : URLSessionDelegate {

    public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

         let urlCredential = URLCredential(trust: challenge.protectionSpace.serverTrust!)

         completionHandler(.useCredential, urlCredential)
    }
}