HTTPS request how to skip SSL verification

The picture above is my code to make a HTTP request, and it is successful.


However, still with this way of writing, if I make an HTTPS request (secure connection), it will fail, with error “the certificate for this server is invalid”.

I know that this error is because SSL usually needs a license for connection so as to verify. But, it is also possible to disable this verification. For example, in python, just import ssl and ssl._create_default_https_context = ssl._create_unverified_context

So, in Swift, is there a sentence that do similar things? To allow SSL HTTPS request without verification?

iPad Air, iPadOS 17

Posted on Apr 30, 2024 5:49 AM

Reply
2 replies

Apr 30, 2024 3:23 PM in response to IvenDong

Either way you go with this, you’re going to be learning more about TLS.


You can either try to avoid using TLS, and macOS is going to want to fight about that…


Or you can fix your TLS.


TLS is integrated, and requires no license.


If you don’t want to use a free cert or don’t want to use s purchased cert, either set up and trust your own self-signed certs, or set up and use your own Certificate Authority (CA). Here’s an intro to your own CA:

May 7, 2024 6:22 AM in response to MrHoffman

Thank you anyway. Actually later on I found a solution on jianshu (a Chinese Programming technician blog)


code:

//https://www.jianshu.com/p/81eb390dc4c2


class urlDelegate:NSObject, URLSessionTaskDelegate{


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


        let trustedCertList:[SecCertificate] = []


        let serverTrust: SecTrust = challenge.protectionSpace.serverTrust!


        SecTrustSetAnchorCertificates(serverTrust, trustedCertList as CFArray)


        completionHandler(.useCredential, URLCredential(trust: serverTrust))


    }


}


let delg = urlDelegate()


let session = URLSession.init(configuration: URLSessionConfiguration.default, delegate: delg, delegateQueue: nil)

HTTPS request how to skip SSL verification

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.