Skip to content

Instantly share code, notes, and snippets.

@Daij-Djan
Last active May 23, 2016 17:08
Show Gist options
  • Save Daij-Djan/3267225e2f1bd7e8daeca05317f70982 to your computer and use it in GitHub Desktop.
Save Daij-Djan/3267225e2f1bd7e8daeca05317f70982 to your computer and use it in GitHub Desktop.
show how to properly ignore invalid ssl certificates while leaving other authentication challenges untouuched
//setup a new task
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let task = session.dataTaskWithURL(url, completionHandler: handleUrlCompleted)
task.resume()
....
//url session needs to accept invalid certificates
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}
else {
completionHandler(NSURLSessionAuthChallengeDisposition.PerformDefaultHandling, nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment