<!--
{
  "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/URLCredentialStorage/setDefaultCredential(_:for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLCredentialStorage(im)setDefaultCredential:forProtectionSpace:"
  },
  "title" : "setDefaultCredential(_:for:)"
}
-->

# setDefaultCredential(_:for:)

Sets the default credential for a specified protection space.

```
func setDefaultCredential(_ credential: URLCredential, for space: URLProtectionSpace)
```

## Parameters

`credential`

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

`space`

The protection space whose default credential is being set.

## Discussion

If you override this method, also override [`setDefaultCredential(_:for:task:)`](/documentation/Foundation/URLCredentialStorage/setDefaultCredential(_:for:task:)).

---

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)