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

# setCookie(_:)

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

```
func setCookie(_ cookie: HTTPCookie)
```

## Parameters

`cookie`

The cookie to store.

## Discussion

The cookie replaces an existing cookie with the same name, domain, and path, if one exists in the cookie storage. This method accepts the cookie only if the storage’s cookie accept policy is [`HTTPCookie.AcceptPolicy.always`](/documentation/Foundation/HTTPCookie/AcceptPolicy/always) or [`HTTPCookie.AcceptPolicy.onlyFromMainDocumentDomain`](/documentation/Foundation/HTTPCookie/AcceptPolicy/onlyFromMainDocumentDomain). The cookie is ignored if the storage’s cookie accept policy is [`HTTPCookie.AcceptPolicy.never`](/documentation/Foundation/HTTPCookie/AcceptPolicy/never).

---

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)