URLSession implementing URLSessionDelegate to bypass any certificate verification

28 Views Asked by At

I have an iOS framework that is a networking module. I need to bypass certificates, but every solution I find is always based on this one, which didn't work for my situation.

extension FCSession: URLSessionDelegate {
    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        
        switch challenge.protectionSpace.authenticationMethod {
        case NSURLAuthenticationMethodServerTrust:
            if let serverTrust = challenge.protectionSpace.serverTrust {
                let credential = URLCredential(trust: serverTrust)
                
                completionHandler(.useCredential, credential)
            }
        default:
            completionHandler(.rejectProtectionSpace, nil)
        }
    }
}

This is log of error:

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=(
    "<cert(0x10880ac00)

I've already tried passing the completionHandler directly with the serverTrust, and receives a same result error.

completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))

0

There are 0 best solutions below