Instance Method

urlSession(_:task:didReceive:completionHandler:)

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

Declaration

optional func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

Parameters

session

The session containing the task whose request requires authentication.

task

The task whose request requires authentication.

challenge

An object that contains the request for authentication.

completionHandler

A handler that your delegate method must call. Its parameters are:

  • disposition—One of several constants that describes how the challenge should be handled.

  • credential—The credential that should be used for authentication if disposition is NSURLSessionAuthChallengeUseCredential; otherwise, NULL.

Discussion

This method handles task-level authentication challenges. The URLSessionDelegate protocol also provides a session-level authentication delegate method. The method called depends on the type of authentication challenge: