URLSession delegates not called after authentication challenge
<p>I'm making a post call to a server and I kept getting the error that the server certificate was invalid, so after researching I found that i must trust the server using authentication challenge. This goes through perfectly fine (I print the authentication challenge to make sure it does) but the rest of the call does not go through and i cannot get information back from my server, nothing happens after the authentication challenge, normally I'd expect to be returned data. However nothing happens and none of my urlsessiondatadelegates are called (checked with breaks). </p>

<p>Can anyone spot why it may be? Code: </p>

<pre><code> var request = URLRequest(url: URL(string: "https://45.56.105.97:7915/search-v2")!)
request.httpMethod = "POST"
request.addValue("Basic ***********", forHTTPHeaderField: "Authorization")

 let task = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
            task.dataTask(with: request) { (data, response, error) in
               print(error)
                print(data)
                if let responseData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
                                    print("data: \(responseData)")
                                    self.parseXML(data!)

                                }
        }.resume()
</code></pre>

<p>delegate methods:</p>

<pre><code>func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -&gt; Void) {
    print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust  {
        print("send credential Server Trust")
        let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        challenge.sender!.use(credential, for: challenge)

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
        print("send credential HTTP Basic")
        let defaultCredentials: URLCredential = URLCredential(user: "username", password: "password", persistence:URLCredential.Persistence.forSession)
        challenge.sender!.use(defaultCredentials, for: challenge)

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
        print("send credential NTLM")

    } else{
        challenge.sender!.performDefaultHandling!(for: challenge)
    }

}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    print("Data received: \(data)")
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -&gt; Void) {

    print("Response received: \(response)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    print("something went wrong: \(error)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -&gt; Void) {
    print("rep: \(response)")
}
</code></pre>

<p>After running, all i get printed is:</p>

<p>did autherntcationchallenge = NSURLAuthenticationMethodServerTrust
send credential Server Trust</p>

<p>I've also seen conversations about how I can only use either a closure or delegate methods for retrieving data/not both. I've tried everything with no success. </p>

<p>**Also I know I shouldnt just be blindly trusting the server, but I am trying to figure out why I cannot receive data back, I am aware this has serious security flaws. </p>