CapacitorCookies
Capacitor Cookies APIは、document.cookieをネイティブライブラリを使用するようにパッチすることでネイティブのクッキーサポートを提供します。特定のURLでクッキーを変更するメソッドも提供します。このプラグインは@capacitor/coreにバンドルされています。
設定
デフォルトでは、document.cookieをネイティブライブラリを使用するようにパッチする機能は無効になっています。
この機能を有効にしたい場合は、capacitor.configファイルで以下の設定を変更してください。
| プロパティ | 型 | 説明 | デフォルト |
|---|---|---|---|
enabled | boolean | document.cookieをネイティブライブラリを使用するようにパッチする機能を有効にします。 | false |
設定例
capacitor.config.jsonでの設定:
{
"plugins": {
"CapacitorCookies": {
"enabled": true
}
}
}
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();
};
iOSでのサードパーティクッキー
iOS 14以降、デフォルトではサードパーティクッキーを使用できません。iOSでのクッキーサポートを向上させるには、以下の行をInfo.plistファイルに追加してください。最大10個のドメインを追加できます。
<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>
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpCookieMap>
setCookie(...)
setCookie(options: SetCookieOptions) => Promise<void>
Write a cookie to the device.
| Param | Type |
|---|---|
options | |
deleteCookie(...)
deleteCookie(options: DeleteCookieOptions) => Promise<void>
Delete a cookie from the device.
| Param | Type |
|---|---|
options | |
clearCookies(...)
clearCookies(options: ClearCookieOptions) => Promise<void>
Clear cookies from the device at a given URL.
| Param | Type |
|---|---|
options | |
clearAllCookies()
clearAllCookies() => Promise<void>
Clear all cookies on the device.
Interfaces
HttpCookieMap
HttpCookie
| Prop | Type | Description |
|---|---|---|
url | string | The URL of the cookie. |
key | string | The key of the cookie. |
value | string | The value of the cookie. |
HttpCookieExtras
| Prop | Type | Description |
|---|---|---|
path | string | The path to write the cookie to. |
expires | string | The 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'>