---
title: "getCredentials(for:task:completionHandler:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlcredentialstorage/getcredentials(for:task:completionhandler:)"
---

# getCredentials(for:task:completionHandler:)

Gets a dictionary containing the credentials for the specified protection space, on behalf of the given task, and passes the dictionary to the provided completion handler.

## Declaration

```swift
func getCredentials(for protectionSpace: URLProtectionSpace, task: URLSessionTask, completionHandler: @escaping @Sendable ([String : URLCredential]?) -> Void)
```

```swift
func credentials(for protectionSpace: URLProtectionSpace, task: URLSessionTask) async -> [String : URLCredential]?
```

## Parameters

- `protectionSpace`: The protection space whose credentials you want to retrieve.
- `task`: The task accessing the specified protection space.
- `completionHandler`: A completion handler that receives a single argument with the credentials for the specified protection space and task. The dictionary’s keys are user name strings, and the corresponding value is a doc://com.apple.foundation/documentation/Foundation/URLCredential. If no credential has been set for this space, the argument to the completion handler is nil.

## See Also

### Retrieving credentials

- [allCredentials](foundation/urlcredentialstorage/allcredentials.md)
- [credentials(for:)](foundation/urlcredentialstorage/credentials(for:).md)
