<!--
{
  "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/HTTPCookieStorage",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSHTTPCookieStorage"
  },
  "title" : "HTTPCookieStorage"
}
-->

# HTTPCookieStorage

A container that manages the storage of cookies.

```
class HTTPCookieStorage
```

## Overview

Each stored cookie is represented by an instance of the [`HTTPCookie`](/documentation/Foundation/HTTPCookie) class.

### Sharing cookie storage

The persistent cookie storage returned by [`shared`](/documentation/Foundation/HTTPCookieStorage/shared) may be available to app extensions or other apps, subject to the following guidelines:

- iOS — Each app and app extension has a unique data container, meaning  they have separate cookie stores. You can obtain a common cookie storage by using the [`sharedCookieStorage(forGroupContainerIdentifier:)`](/documentation/Foundation/HTTPCookieStorage/sharedCookieStorage(forGroupContainerIdentifier:)) method.
- macOS (non-sandboxed) — As of macOS 10.11, each app has its own cookie storage. Prior to macOS 10.11, a common cookie store is shared among the user’s apps.
- macOS (sandboxed) — Same as iOS.
- <doc://com.apple.documentation/documentation/UIKit/UIWebView> — `UIWebView` instances within an app inherit the parent app’s shared cookie storage.
- <doc://com.apple.documentation/documentation/WebKit/WKWebView> — Each `WKWebView` instance has its own cookie storage. See the <doc://com.apple.documentation/documentation/WebKit/WKHTTPCookieStore> class for more information.

Session cookies (where the cookie object’s [`isSessionOnly`](/documentation/Foundation/HTTPCookie/isSessionOnly) property is <doc://com.apple.documentation/documentation/Swift/true>) are local to a single process and are not shared.

> Note:
> In cases where a cookie storage is shared between processes, changes made to the cookie accept policy affect all currently running apps using the cookie storage.

### Subclassing notes

The [`HTTPCookieStorage`](/documentation/Foundation/HTTPCookieStorage) class is usable as-is, but you can subclass it. For example, you can override the storage methods like [`storeCookies(_:for:)`](/documentation/Foundation/HTTPCookieStorage/storeCookies(_:for:)), [`getCookiesFor(_:completionHandler:)`](/documentation/Foundation/HTTPCookieStorage/getCookiesFor(_:completionHandler:)) to screen which cookies are stored, or reimplement the storage mechanism for security or other reasons.

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

- Retrieving cookies — Override [`getCookiesFor(_:completionHandler:)`](/documentation/Foundation/HTTPCookieStorage/getCookiesFor(_:completionHandler:)), instead of or in addition to [`cookies(for:)`](/documentation/Foundation/HTTPCookieStorage/cookies(for:)).
- Adding cookies — Override [`storeCookies(_:for:)`](/documentation/Foundation/HTTPCookieStorage/storeCookies(_:for:)), instead of or in addition to [`setCookies(_:for:mainDocumentURL:)`](/documentation/Foundation/HTTPCookieStorage/setCookies(_:for:mainDocumentURL:)).

## Topics

### Getting the shared cookie storage object

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

The shared cookie storage instance.

[`sharedCookieStorage(forGroupContainerIdentifier:)`](/documentation/Foundation/HTTPCookieStorage/sharedCookieStorage(forGroupContainerIdentifier:))

Returns the cookie storage instance for the container associated with the specified app group identifier.

### Getting and setting the cookie accept policy

[`cookieAcceptPolicy`](/documentation/Foundation/HTTPCookieStorage/cookieAcceptPolicy)

The cookie storage’s cookie accept policy.

[`HTTPCookie.AcceptPolicy`](/documentation/Foundation/HTTPCookie/AcceptPolicy)

Cookie acceptance policies implemented by the [`HTTPCookieStorage`](/documentation/Foundation/HTTPCookieStorage) class.

### Adding and removing cookies

[`removeCookies(since:)`](/documentation/Foundation/HTTPCookieStorage/removeCookies(since:))

Removes cookies that were stored after a given date.

[`deleteCookie(_:)`](/documentation/Foundation/HTTPCookieStorage/deleteCookie(_:))

Deletes the specified cookie from the cookie storage.

[`setCookie(_:)`](/documentation/Foundation/HTTPCookieStorage/setCookie(_:))

Stores a specified cookie in the cookie storage if the cookie accept policy permits.

[`setCookies(_:for:mainDocumentURL:)`](/documentation/Foundation/HTTPCookieStorage/setCookies(_:for:mainDocumentURL:))

Adds an array of cookies to the cookie storage if the storage’s cookie acceptance policy permits.

[`storeCookies(_:for:)`](/documentation/Foundation/HTTPCookieStorage/storeCookies(_:for:))

Stores an array of cookies in the cookie storage, on behalf of the provided task, if the cookie accept policy permits.

### Retrieving cookies

[`cookies`](/documentation/Foundation/HTTPCookieStorage/cookies)

The cookie storage’s cookies.

[`getCookiesFor(_:completionHandler:)`](/documentation/Foundation/HTTPCookieStorage/getCookiesFor(_:completionHandler:))

Fetches cookies relevant to the specified task and passes them to the completion handler.

[`cookies(for:)`](/documentation/Foundation/HTTPCookieStorage/cookies(for:))

Returns all the cookie storage’s cookies that are sent to a specified URL.

[`sortedCookies(using:)`](/documentation/Foundation/HTTPCookieStorage/sortedCookies(using:))

Returns all of the cookie storage’s cookies, sorted according to a given set of sort descriptors.

### Tracking cookie storage changes

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

A notification posted when the cookies stored in the cookie storage have changed.

[`HTTPCookieStorage.CookiesChangedMessage`](/documentation/Foundation/HTTPCookieStorage/CookiesChangedMessage)

A message a cookie storage instance sends when its cookies change.

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

A notification posted when the acceptance policy of the cookie storage has changed.



---

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)