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

# getDefaultCredential(for:task:completionHandler:)

Gets the default credential for the specified protection space, which is being accessed by the given task, and passes it to the provided completion handler.

## Declaration

```swift
func getDefaultCredential(for space: URLProtectionSpace, task: URLSessionTask, completionHandler: @escaping @Sendable (URLCredential?) -> Void)
```

```swift
func defaultCredential(for space: URLProtectionSpace, task: URLSessionTask) async -> URLCredential?
```

## Parameters

- `space`: The protection space of interest.
- `task`: The task seeking to use the protection space
- `completionHandler`: A completion handler that receives the default credential as its argument, or nil if there is no default credential for this combination of protection space and task.

## See Also

### Getting and setting default credentials

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