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

# setDefaultCredential(_:for:task:)

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

## Declaration

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

### Getting and setting default credentials

- [defaultCredential(for:)](foundation/urlcredentialstorage/defaultcredential(for:).md)
- [getDefaultCredential(for:task:completionHandler:)](foundation/urlcredentialstorage/getdefaultcredential(for:task:completionhandler:).md)
- [setDefaultCredential(_:for:)](foundation/urlcredentialstorage/setdefaultcredential(_:for:).md)
