0

I know this type of question has been asked many times but it seems like there is an answer but I can not get it working.

In my environment, production is the only place I have a valid SSL certificate. Using Alamofire in my iOS app built against production works great. I don't want to develop/test against production and we can't get a valid SSL cert right now for any other environment.

I found this SO answer from an Alamofire developer suggesting a way to solve this but it is not working for me. When I run my app against non-production, the failure I'm seeing implies that it is using NSURLConnection and not NSURLSession as the proposed solution seems to rely on.

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

Would love it if someone could share their solution (maybe a gist?) on this.

Community
  • 1
  • 1
ShatyUT
  • 1,375
  • 2
  • 14
  • 28

1 Answers1

1

I finally figured it out. The SO answer I had referenced had a small but important bug in it. I submitted an edit for it but will post the working code here:

let manager = Alamofire.Manager.sharedInstance

manager.delegate.sessionDidReceiveChallenge = { session, challenge in
    var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
    var credential: NSURLCredential?

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
        disposition = NSURLSessionAuthChallengeDisposition.UseCredential
        credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
    } else {
        if challenge.previousFailureCount > 0 {
            disposition = .CancelAuthenticationChallenge
        } else {
            credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)

            if credential != nil {
                disposition = .UseCredential
            }
        }
    }

    return (disposition, credential)
}

The issue was that it was not using the singleton instance of Alamofire.Manager. Instead it was creating a brand new instance which was never being used by the Alamofire framework.

ShatyUT
  • 1,375
  • 2
  • 14
  • 28