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

# set(_:for:)

Adds a credential to the credential storage for the specified protection space.

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

## Parameters

`credential`

The credential to add. If a credential with the same user name already exists in `space`, then `credential` replaces the existing object.

`space`

The protection space to which to add the credential.

## Discussion

If the credential is not yet in the set for the protection space, it will be added to it.

If you override this method, also override [`set(_:for:task:)`](/documentation/Foundation/URLCredentialStorage/set(_: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)