I do not agree with the comments about a simple upgrade to the latest version for the solution. It is best to deploy zero-check options before other comparisons. I would rewrite your code (albeit a little more verbose than necessary) as follows, which should also fix your problem in all versions:
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
{
if let authenticationMethod = protectionSpace?.authenticationMethod
{
return authenticationMethod == NSURLAuthenticationMethodServerTrust
}
return false
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge?)
{
if let authenticationMethod = challenge?.protectionSpace.authenticationMethod
{
if authenticationMethod == NSURLAuthenticationMethodServerTrust
{
if challenge?.protectionSpace.host == "www.myhost.com"
{
let credentials = NSURLCredential(forTrust: challenge!.protectionSpace.serverTrust)
challenge!.sender.useCredential(credentials, forAuthenticationChallenge: challenge)
}
}
}
challenge?.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
source
share