brunch

You can make anything
by writing

C.S.Lewis

by Wade Apr 28. 2017

URLSession credentials 관련 처리

kCFStreamErrorDomainSSL 9843 에러

* Swift 3.0 기준.


파일 url (https) 로 URLSession 을 사용해 다운로드를 받으려 할때 credentials 관련 오류를 접하면 아래와 같이 해결하자. kCFStreamErrorDomainSSL -9843 에러 발생 경우


let request = NSMutableURLRequest(url: url)

let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in

...

}


위와 같은 코드를 사용했을때 발생했으며

다음과 같이 코드를 수정해주자.


let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.current)

let task = session.dataTask(with: request as URLRequest){ data,response,error in

...

}


URLSessionDelegate 구현


func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void){

        var disposition: URLSession.AuthChallengeDisposition = URLSession.AuthChallengeDisposition.performDefaultHandling

        var credential:URLCredential?

        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {

            credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)

            if (credential != nil) {

                disposition = URLSession.AuthChallengeDisposition.useCredential

            }

            else{

                disposition = URLSession.AuthChallengeDisposition.performDefaultHandling

            }

        }

        else{

            disposition = URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge

        }

        completionHandler(disposition, credential)

    }


이제 정상적으로 파일을 다운받을 수 있다.


참고 링크

http://stackoverflow.com/questions/40469529/swift-3-urlsession-with-urlcredential-not-working

https://github.com/ankitthakur/SwiftNetwork


매거진의 이전글 Firebase, #iOS Signup

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari