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

# connection(_:willSendRequestFor:)

Tells the delegate that the connection will send a request for an authentication challenge.

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

## Parameters

`connection`

The connection sending the message.

`challenge`

The authentication challenge for which a request is being sent.

## Discussion

This method allows the delegate to make an informed decision about connection authentication at once. If the delegate implements this method, it has no need to implement [`connection(_:canAuthenticateAgainstProtectionSpace:)`](/documentation/Foundation/NSURLConnectionDelegate/connection(_:canAuthenticateAgainstProtectionSpace:)) or [`connection(_:didReceive:)`](/documentation/Foundation/NSURLConnectionDelegate/connection(_:didReceive:)). In fact, those other methods are not invoked (except on older operating systems, where applicable).

In this method,you *must* invoke one of the challenge-responder methods ([`URLAuthenticationChallengeSender`](/documentation/Foundation/URLAuthenticationChallengeSender) protocol):

- [`use(_:for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/use(_:for:))
- [`continueWithoutCredential(for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/continueWithoutCredential(for:))
- [`cancel(_:)`](/documentation/Foundation/URLAuthenticationChallengeSender/cancel(_:))
- [`performDefaultHandling(for:)`](/documentation/Foundation/URLAuthenticationChallengeSender/performDefaultHandling(for:))
- [`rejectProtectionSpaceAndContinue(with:)`](/documentation/Foundation/URLAuthenticationChallengeSender/rejectProtectionSpaceAndContinue(with:))

> Important:
> Your delegate method is called on the thread where the connection is scheduled. Always call the methods above on that same thread.

You might also want to analyze `challenge` for the authentication scheme and the proposed credential before calling a [`URLAuthenticationChallengeSender`](/documentation/Foundation/URLAuthenticationChallengeSender) method. You should never assume that a proposed credential is present. You can either create your own credential and respond with that, or you can send the proposed credential back. (Because this object is immutable, if you want to change it you must copy it and then modify the copy.)

## See Also

[URL Loading System](/documentation/Foundation/url-loading-system)

Interact with URLs and communicate with servers using standard Internet protocols.



---

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)