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

前端 未结 5 642
栀梦
栀梦 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:44

    I received the error "The certificate for this server is invalid. You might be connecting to a server that is pretending to be "www.yoururl.com" which could put your confidential information at risk."

    I solved this by having doing this in my httpclient file. The following code will ignore the authentication request completely.

    var session: URLSession?
    
    session = URLSession(configuration: sessionConfiguration(), delegate: self, delegateQueue: nil)
    
    private func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition) -> Void) {
                completionHandler(
                    .cancelAuthenticationChallenge
                )
            }
    

    Also not sure if it impacted the above but In my info.plist I had "Allow Transport Security Settings" & the child key-value option "Allow Arbitrary Loads" which I set to YES.

提交回复
热议问题