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

# set(_:for:task:)

Adds a credential to the credential storage for the specified protection space, on behalf of the specified task.

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

## Parameters

`credential`

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

`protectionSpace`

The protection space to which to add the credential.

`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)