인디노트

사설 SSL 적용 HTTPS 접속하기 위해서 (Swift) 본문

소스 팁/Objective C, Swift, iOS, macOS

사설 SSL 적용 HTTPS 접속하기 위해서 (Swift)

인디개발자 2022. 12. 8. 15:59

NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “10.10.145.123” which could put your confidential information at risk.

다음과 같은 구현을 해 주자.

extension URLSessionDelegate { 
    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) { 
        if challenge.previousFailureCount > 0 { 
            completionHandler(.cancelAuthenticationChallenge, nil) 
        } else if let serverTrust = challenge.protectionSpace.serverTrust { 
            completionHandler(.useCredential, URLCredential(trust: serverTrust)) 
        } else { 
            print("unknown state. error: \(challenge.error)") 
            completionHandler(.performDefaultHandling, nil) 
        } 
    } 
}
반응형
Comments