Connect to a Server with Invalid Certificate using NSURLSession (swift2,xcode7,ios9)

前端 未结 5 660
栀梦
栀梦 2021-01-13 05:55

I\'m using Xcode 7, Swift 2, and iOS9. I want to connect to a web service using NSURLSession but I get the following error when I try to connect:

2015-10-13          


        
5条回答
  •  不知归路
    2021-01-13 06:31

    Take a look at this article.Shipping an App With App Transport Security particularly the sections about self-signed certificates.

    You'll most likely need the delegate method of the form,

    func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        completionHandler(
            .UseCredential, 
            NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
        )
    }
    

    Adding this to my own comms class that uses NSURLSession fixed the issue.

提交回复
热议问题