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
and will be enabled by default.
Example
import { CapacitorCookies } from '@capacitor/core';
const getCookies = () => {
return document.cookie;
};
const setCookie = () => {
document.cookie = key + '=' + value;
};
const setCapacitorCookie = async () => {
await CapacitorCookies.setCookie({ url, key, value, path, expires });
};
const deleteCookie = async () => {
await CapacitorCookies.deleteCookie({ url, key });
};
const clearCookiesOnUrl = async () => {
await CapacitorCookies.clearCookies({ url });
};
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.
API
setCookie(...)
setCookie(options: SetCookieOptions) => Promise<void>
Write a cookie to the device.
Param | Type |
---|---|
options | SetCookieOptions |
deleteCookie(...)
deleteCookie(options: DeleteCookieOptions) => Promise<void>
Delete a cookie from the device.
Param | Type |
---|---|
options | DeleteCookieOptions |
clearCookies(...)
clearCookies(options: ClearCookieOptions) => Promise<void>
Clear cookies from the device at a given URL.
Param | Type |
---|---|
options | ClearCookieOptions |
clearAllCookies()
clearAllCookies() => Promise<void>
Clear all cookies on the device.
Interfaces
SetCookieOptions
Prop | Type | Description |
---|---|---|
url? | string | The URL to write the cookie to. |
key | string | The key to give the cookie. |
value | string | The value to give the cookie. |
path? | string | The path to write the cookie to. |
expires? | string | The date to expire the cookie. |
DeleteCookieOptions
Prop | Type | Description |
---|---|---|
url? | string | The URL to delete the cookie from. |
key | string | The key of the cookie to delete. |
ClearCookieOptions
Prop | Type | Description |
---|---|---|
url? | string | The URL to clear cookies from. |