Thanks to @Tarun, this codes work
func urlSession(_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?)
{
guard let serverTrust = challenge.protectionSpace.serverTrust,
let certificates = SecTrustCopyCertificateChain(serverTrust) as? [SecCertificate],
let certificate = certificates.first
else {
return (.cancelAuthenticationChallenge, nil)
}
let credential = URLCredential.init(trust: serverTrust)
guard SecTrustEvaluateWithError(serverTrust, nil) else {
return (.cancelAuthenticationChallenge, credential)
}
let cfdata = SecCertificateCopyData(certificate)
...