NSURLSession didReceiveChallenge没有被调用错误(kCFStreamErrorDomainSSL,-9813)

时间:2016-06-29 10:17:23

标签: ios swift

我正在尝试发出NSURLSession请求并且我一直收到此错误(kCFStreamErrorDomainSSL,-9813),下面是我的代码 -

let urlPath = "https://myurl"
    func jsonParser() {
            guard let endpoint = NSURL(string: urlPath) else {
                print("Error creating endpoint")
                return
            }
            let request = NSMutableURLRequest(URL:endpoint)
            NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
                do {
                    guard let data = data else {
                        throw JSONError.NoData
                    }
                    guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary else {
                        throw JSONError.ConversionFailed
                    }
                    print(json)
                } catch let error as JSONError {
                    print(error.rawValue)
                } catch let error as NSError {
                    print(error.debugDescription)
                }
                }.resume()
        }

为了解决这个错误,我添加了这段代码,但不知怎的,这下面的代码甚至没有被调用,我仍然得到同样的错误 -

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {


        if let xmtURL = urlPath, xmtHost = xmtURL.host {
            if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust &&
                challenge.protectionSpace.host == xmtHost {
                let myCredential = NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
                completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, myCredential)
            }
        }


    }

请帮忙。 谢谢!

0 个答案:

没有答案