Skip to main content
Version: v7

CapacitorCookies

The Capacitor Cookies API provides native cookie support via patching document.cookie to use native libraries. It also provides methods for modifying cookies at a specific url. This plugin is bundled with @capacitor/core.

Configurationโ€‹

By default, the patching of document.cookie to use native libraries is disabled. If you would like to enable this feature, modify the configuration below in the capacitor.config file.

PropTypeDescriptionDefault
enabledbooleanEnable the patching of document.cookie to use native libraries instead.false

Example Configurationโ€‹

In capacitor.config.json:

{
"plugins": {
"CapacitorCookies": {
"enabled": true
}
}
}

In capacitor.config.ts:

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
plugins: {
CapacitorCookies: {
enabled: true,
},
},
};

export default config;

Exampleโ€‹

import { CapacitorCookies } from '@capacitor/core';

const getCookies = () => {
return document.cookie;
};

const setCookie = () => {
document.cookie = key + '=' + value;
};

const setCapacitorCookie = async () => {
await CapacitorCookies.setCookie({
url: 'http://example.com',
key: 'language',
value: 'en',
});
};

const deleteCookie = async () => {
await CapacitorCookies.deleteCookie({
url: 'https://example.com',
key: 'language',
});
};

const clearCookiesOnUrl = async () => {
await CapacitorCookies.clearCookies({
url: 'https://example.com',
});
};

const clearAllCookies = async () => {
await CapacitorCookies.clearAllCookies();
};

Third Party Cookies on iOSโ€‹

As of iOS 14, you cannot use 3rd party cookies by default. Add the following lines to your Info.plist file to get better support for cookies on iOS. You can add up to 10 domains.

<key>WKAppBoundDomains</key>
<array>
<string>www.mydomain.com</string>
<string>api.mydomain.com</string>
<string>www.myothercooldomain.com</string>
</array>

APIโ€‹

getCookies(...)โ€‹

getCookies(options?: GetCookieOptions) => Promise<HttpCookieMap>
ParamType
options
GetCookieOptions

Returns:

Promise<HttpCookieMap>


setCookie(...)โ€‹

setCookie(options: SetCookieOptions) => Promise<void>

Write a cookie to the device.

ParamType
options
SetCookieOptions

deleteCookie(...)โ€‹

deleteCookie(options: DeleteCookieOptions) => Promise<void>

Delete a cookie from the device.

ParamType
options
DeleteCookieOptions

clearCookies(...)โ€‹

clearCookies(options: ClearCookieOptions) => Promise<void>

Clear cookies from the device at a given URL.

ParamType
options
ClearCookieOptions

clearAllCookies()โ€‹

clearAllCookies() => Promise<void>

Clear all cookies on the device.


Interfacesโ€‹

HttpCookieMapโ€‹

HttpCookieโ€‹

PropTypeDescription
urlstringThe URL of the cookie.
keystringThe key of the cookie.
valuestringThe value of the cookie.

HttpCookieExtrasโ€‹

PropTypeDescription
pathstringThe path to write the cookie to.
expiresstringThe date to expire the cookie.

Type Aliasesโ€‹

GetCookieOptionsโ€‹

Omit<HttpCookie, 'key' | 'value'>

Omitโ€‹

Construct a type with the properties of T except for those in type K.

Pick<T, Exclude<keyof T, K>>

Pickโ€‹

From T, pick a set of properties whose keys are in the union K

{ [P in K]: T[P]; }

Excludeโ€‹

Exclude from T those types that are assignable to U

T extends U ? never : T

SetCookieOptionsโ€‹

HttpCookie & HttpCookieExtras

DeleteCookieOptionsโ€‹

Omit<HttpCookie, 'value'>

ClearCookieOptionsโ€‹

Omit<HttpCookie, 'key' | 'value'>