<!--
{
  "availability" : [
    "macOS: 10.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURLDownloadDelegate/download(_:didReceive:)-1pc0v",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLDownloadDelegate(im)download:didReceiveAuthenticationChallenge:"
  },
  "title" : "download(_:didReceive:)"
}
-->

# download(_:didReceive:)

Sent when the URL download must authenticate a challenge in order to download the request.

```
optional func download(_ download: NSURLDownload, didReceive challenge: URLAuthenticationChallenge)
```

## Parameters

`download`

The URL download object sending the message.

`challenge`

The URL authentication challenge that must be authenticated in order to download the request.

## Discussion

This method gives the delegate the opportunity to determine the course of action taken for the challenge: provide credentials, continue without providing credentials or cancel the authentication challenge and the download.

The delegate can determine the number of previous authentication challenges by sending the message [`previousFailureCount`](/documentation/Foundation/URLAuthenticationChallenge/previousFailureCount) to `challenge`.

If the previous failure count is 0 and the value returned by [`proposedCredential`](/documentation/Foundation/URLAuthenticationChallenge/proposedCredential) is `nil`, the delegate can create a new NSURLCredential object, providing information specific to the type of credential, and send a [`use(_:for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/use(_:for:)) message to `[challenge sender]`, passing the credential and `challenge` as parameters. If [`proposedCredential`](/documentation/Foundation/URLAuthenticationChallenge/proposedCredential) is not `nil`, the value is a credential from the URL or the shared credential storage that can be provided to the user as feedback.

The delegate may decide to abandon further attempts at authentication at any time by sending `[challenge sender]` a [`continueWithoutCredential(for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/continueWithoutCredential(for:)) or a [`cancel(_:)`](/documentation/Foundation/URLAuthenticationChallengeSender/cancel(_:)) message. The specific action is implementation dependent.

If the delegate implements this method, the download will suspend until `[challenge sender]` is sent one of the following messages: [`use(_:for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/use(_:for:)), [`continueWithoutCredential(for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/continueWithoutCredential(for:)) or [`cancel(_:)`](/documentation/Foundation/URLAuthenticationChallengeSender/cancel(_:)).

If the delegate does not implement this method the default implementation is used. If a valid credential for the request is provided as part of the URL, or is available from the NSURLCredentialStorage the `[challenge sender]` is sent a [`use(_:for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/use(_:for:)) with the credential. If the challenge has no credential or the credentials fail to authorize access, then [`continueWithoutCredential(for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/continueWithoutCredential(for:)) is sent to `[challenge sender]` instead.

---

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)