<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.2.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLCredentialStorage",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLCredentialStorage"
  },
  "title" : "URLCredentialStorage"
}
-->

# URLCredentialStorage

The manager of a shared credentials cache.

```
class URLCredentialStorage
```

## Overview

The shared cache stores and retrieves instances of [`URLCredential`](/documentation/Foundation/URLCredential). You can store password-based credentials permanently, based on the [`URLCredential.Persistence`](/documentation/Foundation/URLCredential/Persistence-swift.enum) they were created with. Certificate-based credentials are never stored permanently.

### Subclassing notes

The [`URLCredentialStorage`](/documentation/Foundation/URLCredentialStorage) class is meant to be used as-is, but you can subclass it if you have specific needs, such as screening which credentials are stored.

When overriding methods of this class, be aware that methods that take a `task` parameter are preferred to equivalent methods that do not. Therefore, you should override the task-based methods when subclassing, as follows:

- Setting credentials — Override [`set(_:for:task:)`](/documentation/Foundation/URLCredentialStorage/set(_:for:task:)) instead of or in addition to [`set(_:for:)`](/documentation/Foundation/URLCredentialStorage/set(_:for:)).
- Getting credentials — Override [`getCredentials(for:task:completionHandler:)`](/documentation/Foundation/URLCredentialStorage/getCredentials(for:task:completionHandler:)) instead of or in addition to [`credentials(for:)`](/documentation/Foundation/URLCredentialStorage/credentials(for:)).
- Removing credentials — Override [`remove(_:for:options:task:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:options:task:)) instead of or in addition to [`remove(_:for:options:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:options:)) and [`remove(_:for:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:)).
- Setting default credentials — Override [`setDefaultCredential(_:for:task:)`](/documentation/Foundation/URLCredentialStorage/setDefaultCredential(_:for:task:)) instead of or in addition to [`setDefaultCredential(_:for:)`](/documentation/Foundation/URLCredentialStorage/setDefaultCredential(_:for:)).
- Getting default credentials — Override [`getDefaultCredential(for:task:completionHandler:)`](/documentation/Foundation/URLCredentialStorage/getDefaultCredential(for:task:completionHandler:)) instead of or in addition to [`defaultCredential(for:)`](/documentation/Foundation/URLCredentialStorage/defaultCredential(for:)).

## Topics

### Getting the credential storage

[`shared`](/documentation/Foundation/URLCredentialStorage/shared)

The shared URL credential storage instance.

### Getting and setting default credentials

[`defaultCredential(for:)`](/documentation/Foundation/URLCredentialStorage/defaultCredential(for:))

Returns the default credential for the specified protection space.

[`getDefaultCredential(for:task:completionHandler:)`](/documentation/Foundation/URLCredentialStorage/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.

[`setDefaultCredential(_:for:)`](/documentation/Foundation/URLCredentialStorage/setDefaultCredential(_:for:))

Sets the default credential for a specified protection space.

[`setDefaultCredential(_:for:task:)`](/documentation/Foundation/URLCredentialStorage/setDefaultCredential(_:for:task:))

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

### Adding and removing credentials

[`remove(_:for:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:))

Removes the specified credential from the credential storage for the specified protection space.

[`remove(_:for:options:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:options:))

Removes the specified credential from the credential storage for the specified protection space using the given options.

[`remove(_:for:options:task:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:options:task:))

Removes the specified credential from the credential storage for the specified protection space, on behalf of the given task and using the given options.

[Dictionary key for credential removal options](/documentation/Foundation/dictionary-key-for-credential-removal-options)

Key used by the options dictionary passed in [`remove(_:for:options:)`](/documentation/Foundation/URLCredentialStorage/remove(_:for:options:)).

[`set(_:for:)`](/documentation/Foundation/URLCredentialStorage/set(_:for:))

Adds a credential to the credential storage for the specified protection space.

[`set(_:for:task:)`](/documentation/Foundation/URLCredentialStorage/set(_:for:task:))

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

### Retrieving credentials

[`allCredentials`](/documentation/Foundation/URLCredentialStorage/allCredentials)

The credentials for all available protection spaces.

[`credentials(for:)`](/documentation/Foundation/URLCredentialStorage/credentials(for:))

Returns a dictionary containing the credentials for the specified protection space.

[`getCredentials(for:task:completionHandler:)`](/documentation/Foundation/URLCredentialStorage/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.

### Tracking credential storage changes

[`NSURLCredentialStorageChanged`](/documentation/Foundation/NSNotification/Name-swift.struct/NSURLCredentialStorageChanged)

A notification posted when the set of stored credentials changes.



---

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)