NSHTTPCookieStorage implements a singleton object (shared instance) that manages storage of cookies. Each cookie is represented by an instance of the HTTPCookie class. As a rule, cookies are shared among all applications and are kept in sync across process boundaries. Session cookies (where the cookie object’s isSessionOnly method returns true) are local to a single process and are not shared.


Thread Safety

In macOS 10.9 and later and iOS 7 and later, NSHTTPCookieStorage is thread safe.


Getting the Shared Cookie Storage Object

Getting and Setting the Cookie Accept Policy

var cookieAcceptPolicy: HTTPCookie.AcceptPolicy

The cookie storage’s cookie accept policy.

Adding and Removing Cookies

func deleteCookie(HTTPCookie)

Deletes the specified cookie from the cookie storage.

func setCookie(HTTPCookie)

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

func setCookies([HTTPCookie], for: URL?, mainDocumentURL: URL?)

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

Retrieving Cookies

var cookies: [HTTPCookie]?

The cookie storage’s cookies.

func cookies(for: URL)

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

func sortedCookies(using: [NSSortDescriptor])

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


enum HTTPCookie.AcceptPolicy

NSHTTPCookieAcceptPolicy specifies the cookie acceptance policies implemented by the NSHTTPCookieStorage class.


static let NSHTTPCookieManagerCookiesChanged: NSNotification.Name

This notification is posted when the cookies stored in the NSHTTPCookieStorage instance have changed.

static let NSHTTPCookieManagerAcceptPolicyChanged: NSNotification.Name

This notification is posted when the acceptance policy of the NSHTTPCookieStorage instance has changed.


Inherits From

Conforms To

See Also

Setting Cookie Policies

var httpCookieAcceptPolicy: HTTPCookie.AcceptPolicy

A policy constant that determines when cookies should be accepted.

var httpShouldSetCookies: Bool

A Boolean value that determines whether requests should contain cookies from the cookie store.

var httpCookieStorage: HTTPCookieStorage?

The cookie store for storing cookies within this session.

class HTTPCookie

An NSHTTPCookie object represents an HTTP cookie. It is an immutable object, initialized from a dictionary containing the cookie attributes.