<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionDelegate/urlSession(_:didReceive:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionDelegate(im)URLSession:didReceiveChallenge:completionHandler:"
  },
  "title" : "urlSession(_:didReceive:completionHandler:)"
}
-->

# urlSession(_:didReceive:completionHandler:)

Requests credentials from the delegate in response to a session-level authentication request from the remote server.

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

## Parameters

`session`

The session containing the task that requested authentication.

`challenge`

An object that contains the request for authentication.

`completionHandler`

A handler that your delegate method must call. This completion handler takes the following parameters::

- `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 is called in two situations:

- When a remote server asks for client certificates or Windows NT LAN Manager (NTLM) authentication, to allow your app to provide appropriate credentials
- When a session first establishes a connection to a remote server that uses SSL or TLS, to allow your app to verify the server’s certificate chain

If you do not implement this method, the session calls its delegate’s [`urlSession(_:task:didReceive:completionHandler:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didReceive:completionHandler:)) method instead.

> Note:
> This method handles *only* the ``doc://com.apple.foundation/documentation/Foundation/NSURLAuthenticationMethodNTLM``, ``doc://com.apple.foundation/documentation/Foundation/NSURLAuthenticationMethodNegotiate``, ``doc://com.apple.foundation/documentation/Foundation/NSURLAuthenticationMethodClientCertificate``, and ``doc://com.apple.foundation/documentation/Foundation/NSURLAuthenticationMethodServerTrust`` authentication types. For all other authentication schemes, the session calls *only* the ``doc://com.apple.foundation/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didReceive:completionHandler:)`` method.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)