some self-signed certificates and just connect to the network. I don't have any sensitive information in my app. I want to disable the check per connection not for all. I am new to iOS development please help. Will Apple reject the app because of that?
You don’t need to customise HTTPS server trust evaluation to determine if the server is using a self-signed certificate. Even with ATS enabled, you still get a
NSURLAuthenticationMethodServerTrust authentication challenge and you can make your decision based on the certificates in that challenge. At the end of this response I’ve posted a snippet that shows how to do this. Here’s what I see when I run it:
… 09:21:32… start
… 09:21:32… is self-signed: true
…
… 09:21:32… task transport error NSURLErrorDomain / -1200Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"func test() {
NSLog("start")
let url = URL(string: "https://self-signed.badssl.com")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
self.session.dataTask(with: request) { (data, response, error) in
if let error = error as NSError? {
NSLog("task transport error %@ / %d", error.domain, error.code)
return
}
let response = response as! HTTPURLResponse
let data = data!
NSLog("task finished with status %d, bytes %d", response.statusCode, data.count)
}.resume()
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
let trust = challenge.protectionSpace.serverTrust!
NSLog("is self-signed: %@", trust.isSelfSigned.flatMap { "\($0)" } ?? "unknown" )
}
completionHandler(.performDefaultHandling, nil)
}extension SecTrust {
var isSelfSigned: Bool? {
guard SecTrustGetCertificateCount(self) == 1 else {
return false
}
guard let cert = SecTrustGetCertificateAtIndex(self, 0) else {
return nil
}
return cert.isSelfSigned
}
}
extension SecCertificate {
var isSelfSigned: Bool? {
guard
let subject = SecCertificateCopyNormalizedSubjectSequence(self),
let issuer = SecCertificateCopyNormalizedIssuerSequence(self)
else {
return nil
}
return subject == issuer
}
}