<!--
{
  "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/setCookies(_:for:mainDocumentURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSHTTPCookieStorage(im)setCookies:forURL:mainDocumentURL:"
  },
  "title" : "setCookies(_:for:mainDocumentURL:)"
}
-->

# setCookies(_:for:mainDocumentURL:)

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?)
```

## Parameters

`cookies`

The cookies to add.

`URL`

The URL associated with the added cookies.

`mainDocumentURL`

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`](/documentation/Foundation/HTTPCookie/AcceptPolicy/onlyFromMainDocumentDomain).

## Discussion

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`](/documentation/Foundation/HTTPCookie/AcceptPolicy/never), the cookies are ignored.

To store cookies from a set of response headers, an application can use [`cookies(withResponseHeaderFields:for:)`](/documentation/Foundation/HTTPCookie/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:)`](/documentation/Foundation/HTTPCookieStorage/storeCookies(_:for:)).

---

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)