fatal error: unexpectedly found nil while unwrapping an Optional value with URLSession
<p>I have this method that was working in Swift 2.2 but ever since I converted my code to Swift 3 it no longer works, what this method does is take a username and password login into a URL with Windows Authentication, if the creds are correct it returns true, if they are not correct, it will return false.</p>

<p>Here is the method:</p>

<pre><code>func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -&gt; Void)
    {
        //Setup the NSURLSessionConfiguration

        let configuration = URLSessionConfiguration.default

        //Setup the NSURLSession

        let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

        //Add the username and password to NSURLCredential

        credential = URLCredential(user:username, password:password, persistence: .forSession)

        //Create request URL as String

        let requestString = NSString(format:"%@", webservice) as String

        //Convert URL string to NSURL

        let url: URL! = URL(string: requestString)

        //Prepare the task to get data.

        let task = session.dataTask(with: url, completionHandler: {
            data, response, error in

            DispatchQueue.main.async(execute: {

                if(error == nil)
                {

                    //If there is no error calling the API, return true

                    completion(true)
                }
                else
                {

                    //If there is an error calling the API, return false

                    completion(false)
                }

            })

        })

        //Run the task to get data.

        task.resume()

    }
</code></pre>

<p>and I get this error:</p>

<pre><code>fatal error: unexpectedly found nil while unwrapping an Optional value
</code></pre>

<p>this occurs right here:</p>

<pre><code>let task = session.dataTask(with: url, completionHandler: {
            data, response, error in

            DispatchQueue.main.async(execute: {

                if(error == nil)
                {

                    //If there is no error calling the API, return true

                    completion(true)
                }
                else
                {

                    //If there is an error calling the API, return false

                    completion(false)
                }

            })

        })
</code></pre>

<p>What am I doing wrong?</p>

<p>This appears in my Debug navigator before the fatal error:</p>

<pre><code>function signature specialization &lt;preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () to @callee_owned (@unowned Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; ()]&gt; of generic specialization &lt;preserving fragile attribute, ()&gt; of Swift.StaticString.withUTF8Buffer &lt;A&gt; ((Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; A) -&gt; A
</code></pre>

<p>I believe my problem is here:</p>

<pre><code>/**
     Requests credentials from the delegate in response to a session-level authentication request from the remote server.
     */

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -&gt; Void) {

        if challenge.previousFailureCount &gt; 0
        {
            completionHandler(Foundation.URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
        }
        else
        {
            completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust:challenge.protectionSpace.serverTrust!))
        }

    }

    /**
     Requests credentials from the delegate in response to an authentication request from the remote server.
     */

    func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -&gt; Void) {

        completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential,credential)

    }
</code></pre>

<p>it doesn't like these methods.</p>