Потому что вы должны использовать сертификат.
class ViewController: UIViewController, URLSessionDelegate,URLSessionTaskDelegate {
var urlSession: Foundation.URLSession!
override func viewDidLoad() {
super.viewDidLoad()
urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let serverTrust = challenge.protectionSpace.serverTrust
let certificate = SecTrustGetCertificateAtIndex(serverTrust!, 0)
let policies = NSMutableArray();
policies.add(SecPolicyCreateSSL(true, (challenge.protectionSpace.host as CFString)))
SecTrustSetPolicies(serverTrust!, policies);
var result: SecTrustResultType = SecTrustResultType(rawValue: 0)!
SecTrustEvaluate(serverTrust!, &result)
let isServerTrusted:Bool = (result == SecTrustResultType.unspecified || result == SecTrustResultType.proceed)
let remoteCertificateData:NSData = SecCertificateCopyData(certificate!)
let pathToCert = Bundle.main.path(forResource: "certificateName", ofType: "crt")
let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)!
let credential:URLCredential = URLCredential(trust: serverTrust!)
completionHandler(.useCredential, credential)
}
}