A representation of an HTTP cookie.


class HTTPCookie : NSObject


An HTTPCookie object is immutable, initialized from a dictionary containing the attributes of the cookie. This class supports two different cookie versions:

  • Version 0: The original cookie format defined by Netscape.Most cookies are in this format.

  • Version 1: The cookie format defined in RFC 6265, HTTP State Management Mechanism.


Creating Cookies

class func cookies(withResponseHeaderFields: [String : String], for: URL) -> [HTTPCookie]

Creates an array HTTP cookies corresponding to the provided response header fields for the provided URL.

init?(properties: [HTTPCookiePropertyKey : Any])

Creates an HTTP cookie instance with the given cookie properties.

Converting Cookies to Request Headers

class func requestHeaderFields(with: [HTTPCookie]) -> [String : String]

Converts an array of cookies to a dictionary of header fields.

Getting Cookie Host Properties

var domain: String

The domain of the cookie.

var path: String

The cookie’s path.

var portList: [NSNumber]?

The cookie’s port list.

Getting Cookie Metadata

var name: String

The cookie’s name.

var value: String

The cookie‘s string value.

var version: Int

The cookie’s version.

Determining Cookie Lifespan

var expiresDate: Date?

The cookie’s expiration date.

var isSessionOnly: Bool

A Boolean value that indicates whether the cookie should be discarded at the end of the session (regardless of expiration date).

Securing Cookies

var isHTTPOnly: Bool

A Boolean value that indicates whether the cookie should only be sent to HTTP servers.

var isSecure: Bool

A Boolean value that indicates whether this cookie should only be sent over secure channels.

Accessing Cookie Properties as Key-Value Pairs

struct HTTPCookiePropertyKey

Constants that define the supported keys in a cookie attributes dictionary.

Getting User-Readable Cookie Metadata

var comment: String?

The cookie’s comment string.

var commentURL: URL?

The cookie’s comment URL.

Accepting Cookies

enum HTTPCookie.AcceptPolicy

Cookie acceptance policies implemented by the HTTPCookieStorage class.


Inherits From

Conforms To

See Also


class HTTPCookieStorage

A container that manages the storage of cookies.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software