Instance Method


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


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



The cookies to add.


The URL associated with the added cookies.


The URL of the main HTML document for the top-level frame, if known. The value can be nil. This URL is used to determine whether the cookie should be accepted if the cookie accept policy is HTTPCookie.AcceptPolicy.onlyFromMainDocumentDomain.


Cookies in the array will replace existing cookies with the same name, domain, and path in the cookie storage. If the storage has an accept policy of HTTPCookie.AcceptPolicy.never, the cookies are ignored.

To store cookies from a set of response headers, an application can use cookies(withResponseHeaderFields:for:) passing a header field dictionary and then use this method to store the resulting cookies in accordance with the cookie storage’s cookie acceptance policy.

If you override this method, also override storeCookies(_:for:).

See Also

Adding and Removing Cookies

func removeCookies(since: Date)

Removes cookies that were stored after a given date.

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 storeCookies([HTTPCookie], for: URLSessionTask)

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