<!--
{
  "availability" : [
    "iOS: 2.0.0 - 8.0.0",
    "iPadOS: 2.0.0 - 8.0.0",
    "macCatalyst: 13.1.0 - 13.1.0",
    "macOS: 10.2.0 - 10.10.0",
    "tvOS: 9.0.0 - 9.0.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURLConnectionDelegate/connection(_:didReceive:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLConnectionDelegate(im)connection:didReceiveAuthenticationChallenge:"
  },
  "title" : "connection(_:didReceive:)"
}
-->

# connection(_:didReceive:)

Sent when a connection must authenticate a challenge in order to download its request.

```
optional func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge)
```

## Parameters

`connection`

The connection sending the message.

`challenge`

The challenge that `connection` must authenticate in order to download its 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.

> Note:
> This method is not called if the delegate implements the ``doc://com.apple.foundation/documentation/Foundation/NSURLConnectionDelegate/connection(_:willSendRequestFor:)`` method.

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.

## See Also

[`use(_:for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/use(_:for:))

Attempt to use a given credential for a given authentication challenge.

[`continueWithoutCredential(for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/continueWithoutCredential(for:))

Attempt to continue downloading a request without providing a credential for a given challenge.

[`cancel(_:)`](/documentation/Foundation/URLAuthenticationChallengeSender/cancel(_:))

Cancels a given authentication challenge.



---

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)