<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLCredentialStorage/setDefaultCredential(_:for:task:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLCredentialStorage(im)setDefaultCredential:forProtectionSpace:task:"
  },
  "title" : "setDefaultCredential(_:for:task:)"
}
-->

# setDefaultCredential(_:for:task:)

Sets the default credential for a given protection space, which is being accessed by the given task.

```
func setDefaultCredential(_ credential: URLCredential, for protectionSpace: URLProtectionSpace, task: URLSessionTask)
```

## Parameters

`credential`

The URL credential to set as the default for the protection space. If the receiver does not contain `credential` in the specified protection space it will be added.

`protectionSpace`

The protection space whose default credential is being set.

`task`

The task accessing the specified protection space. Subclasses of [`URLCredentialStorage`](/documentation/Foundation/URLCredentialStorage) may use the request URL or other properties of this task to affect how the default credential is stored.

## Discussion

---

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)