비공인(https) 접속하기
인증서가 유효하지 않은 서버(https)에 접속하게 되면 다음과 같은 error 메시지를 볼수 있다.
The certificate for this server is invalid. You might be connecting to a server that is pretending to be “서버주소” which could put your confidential information at risk.
이럴때 임의로 통과시켜 주기 위해서는 다음과 같이 URLSessionDelegate에서 작업을 해주면 된다.
extension HTTPManager: 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)
}
}
}