<!--
{
  "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/URLAuthenticationChallenge/init(protectionSpace:proposedCredential:previousFailureCount:failureResponse:error:sender:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLAuthenticationChallenge(im)initWithProtectionSpace:proposedCredential:previousFailureCount:failureResponse:error:sender:"
  },
  "title" : "init(protectionSpace:proposedCredential:previousFailureCount:failureResponse:error:sender:)"
}
-->

# init(protectionSpace:proposedCredential:previousFailureCount:failureResponse:error:sender:)

Initializes an authentication challenge from parameters you provide.

```
init(protectionSpace space: URLProtectionSpace, proposedCredential credential: URLCredential?, previousFailureCount: Int, failureResponse response: URLResponse?, error: (any Error)?, sender: any URLAuthenticationChallengeSender)
```

## Parameters

`space`

The protection space for the authentication challenge. This provides additional information about the authentication request, such as the host, port, authentication realm, and so on.

`credential`

The proposed credential, or `nil`.

`previousFailureCount`

The total number of previous failures for this request, including failures for other protection spaces.

`response`

An instance of [`URLResponse`](/documentation/Foundation/URLResponse) containing the server response that caused you to generate an authentication challenge, or `nil` if no response object is applicable to the challenge.

`error`

An `NS``Error` instance describing the authentication failure, or `nil` if it is not applicable to the challenge.

`sender`

The object that initiated the authentication challenge (typically, the object that called this method).

## Return Value

A new authentication challenge object, with the given properties.

## Discussion

Most apps don’t create [`URLAuthenticationChallenge`](/documentation/Foundation/URLAuthenticationChallenge) instances themselves. Instead, they handle received challenges in the [`urlSession(_:task:didReceive:completionHandler:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didReceive:completionHandler:)) method of [`URLSessionTaskDelegate`](/documentation/Foundation/URLSessionTaskDelegate).

However, you might need to create authentication challenge objects when adding support for custom networking protocols, as part of your custom [`URLProtocol`](/documentation/Foundation/URLProtocol) subclasses.

---

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)