0

I was trying to make a NSURLSession request and i kept getting this error (kCFStreamErrorDomainSSL, -9813), below is my code -

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()
        }

TO resolve that error i added this code, but somehow this below code is not even called and i am still getting same error -

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)
            }
        }


    }

Please help. Thanks!

Richa Srivastava
  • 482
  • 1
  • 7
  • 24
  • Have you seen the below link. It might not be the issue because of credential challenge - http://stackoverflow.com/questions/30778579/kcfstreamerrordomainssl-9802-when-connecting-to-a-server-by-ip-address-through –  Jun 29 '16 at 10:39
  • I have plist set like this- NSAppTransportSecurity NSAllowsArbitraryLoads – Richa Srivastava Jun 29 '16 at 10:54
  • Can you elaborate more on credential challenge issue? – Richa Srivastava Jun 29 '16 at 10:55
  • What is the request type, GET or POST? if its post then put the credentials in the Request body and prepare your request (request.setHTTPBody=body). Also specify HTTPMethod and forHTTPHeaderField for. This worked for me –  Jun 29 '16 at 11:35

0 Answers0