---
title: "setCookie(_:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/httpcookiestorage/setcookie(_:)"
---

# setCookie(_:)

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

## Declaration

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

## Parameters

- `cookie`: The cookie to store.

## Discussion

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 or HTTPCookie.AcceptPolicy.onlyFromMainDocumentDomain. The cookie is ignored if the storage’s cookie accept policy is HTTPCookie.AcceptPolicy.never.

## See Also

### Adding and removing cookies

- [removeCookies(since:)](foundation/httpcookiestorage/removecookies(since:).md)
- [deleteCookie(_:)](foundation/httpcookiestorage/deletecookie(_:).md)
- [setCookies(_:for:mainDocumentURL:)](foundation/httpcookiestorage/setcookies(_:for:maindocumenturl:).md)
- [storeCookies(_:for:)](foundation/httpcookiestorage/storecookies(_:for:).md)
