---
title: "set(_:for:task:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlcredentialstorage/set(_:for:task:)"
---

# set(_:for:task:)

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

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/URLCredentialStorage may use the request URL or other properties of this task to affect how the default credential is stored.

## See Also

### Adding and removing credentials

- [remove(_:for:)](foundation/urlcredentialstorage/remove(_:for:).md)
- [remove(_:for:options:)](foundation/urlcredentialstorage/remove(_:for:options:).md)
- [remove(_:for:options:task:)](foundation/urlcredentialstorage/remove(_:for:options:task:).md)
- [Dictionary key for credential removal options](foundation/dictionary-key-for-credential-removal-options.md)
- [set(_:for:)](foundation/urlcredentialstorage/set(_:for:).md)
