A container that manages the storage of cookies.
SDKs
- iOS 2.0+
- macOS 10.2+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
class HTTPCookieStorage : NSObject
Overview
Each stored cookie is represented by an instance of the HTTPCookie
class.
Sharing Cookie Storage
The persistent cookie storage returned by 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
shared
method.Cookie Storage(for Group Container Identifier:) 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.
UIWeb
—View UIWeb
instances within an app inherit the parent app's shared cookie storage.View WKWeb
— EachView WKWeb
instance has its own cookie storage. See theView WKHTTPCookie
class for more information.Store
Session cookies (where the cookie object’s is
property is 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 HTTPCookie
class is usable as-is, but you can subclass it. For example, you can override the storage methods like store
, get
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
get
, instead of or in addition toCookies For(_: completion Handler:) cookies(for:)
.Adding cookies — Override
store
, instead of or in addition toCookies(_: for:) set
.Cookies(_: for: main Document URL:)