# Class (WebCookieManager)
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有Web组件共享一个WebCookieManager实例。
> **说明:**
>
> - 本模块首批接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
>
> - 本Class首批接口从API version 9开始支持。
>
> - 示例效果请以真机运行为准,当前DevEco Studio预览器不支持。
>
> - 静态方法必须在用户界面(UI)线程上使用。
## 导入模块
```ts
import { webview } from '@kit.ArkWeb';
```
## fetchCookieSync11+
static fetchCookieSync(url: string, incognito?: boolean): string
获取指定url对应cookie的值。
> **说明:**
>
> 系统会自动清理过期的cookie,对于同名key的数据,新数据将会覆盖前一个数据。
>
> 为了获取可正常使用的cookie值,fetchCookieSync需传入完整链接。
>
> fetchCookieSync用于获取所有的cookie值,每条cookie值之间会通过"; "进行分隔,但无法单独获取某一条特定的cookie值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
| incognito | boolean | 否 | true表示获取隐私模式下webview的内存cookies,false表示正常非隐私模式下的cookies。
默认值:false。 |
**返回值:**
| 类型 | 说明 |
| ------ | ------------------------- |
| string | 指定url对应的cookie的值。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('fetchCookieSync')
.onClick(() => {
try {
let value = webview.WebCookieManager.fetchCookieSync('https://www.example.com');
console.info("fetchCookieSync cookie = " + value);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## fetchCookie11+
static fetchCookie(url: string, callback: AsyncCallback\): void
异步callback方式获取指定url对应cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
| callback | AsyncCallback\ | 是 | callback回调,用于获取cookie |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 17100002 | URL error. No valid cookie found for the specified URL. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('fetchCookie')
.onClick(() => {
try {
webview.WebCookieManager.fetchCookie('https://www.example.com', (error, cookie) => {
if (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
return;
}
if (cookie) {
console.info('fetchCookie cookie = ' + cookie);
}
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## fetchCookie11+
static fetchCookie(url: string): Promise\
以Promise方式异步获取指定url对应cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
**返回值:**
| 类型 | 说明 |
| ------ | ------------------------- |
| Promise\ | Promise实例,用于获取指定url对应的cookie值。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 17100002 | URL error. No valid cookie found for the specified URL. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('fetchCookie')
.onClick(() => {
try {
webview.WebCookieManager.fetchCookie('https://www.example.com')
.then(cookie => {
console.info("fetchCookie cookie = " + cookie);
})
.catch((error: BusinessError) => {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## fetchCookie14+
static fetchCookie(url: string, incognito: boolean): Promise\
以Promise方式异步获取指定url对应cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
| incognito | boolean | 是 | true表示获取隐私模式下webview的内存cookies,false表示正常非隐私模式下的cookies。 |
**返回值:**
| 类型 | 说明 |
| ------ | ------------------------- |
| Promise\ | Promise实例,用于获取指定url对应的cookie值。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 17100002 | URL error. No valid cookie found for the specified URL. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('fetchCookie')
.onClick(() => {
try {
webview.WebCookieManager.fetchCookie('https://www.example.com', false)
.then(cookie => {
console.info("fetchCookie cookie = " + cookie);
})
.catch((error: BusinessError) => {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## configCookieSync11+
static configCookieSync(url: string, value: string, incognito?: boolean): void
为指定url设置单个cookie的值。
> **说明:**
>
> configCookieSync中的url,可以指定域名的方式来使得页面内请求也附带上cookie。
>
> 同步cookie的时机建议在Web组件加载之前完成。
>
> 若通过configCookieSync进行两次或多次设置cookie,则每次设置的cookie之间会通过"; "进行分隔。
>
> Cookie每30s周期性保存到磁盘中,也可以使用接口[saveCookieAsync](#savecookieasync)进行强制落盘。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要设置的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
| incognito | boolean | 否 | true表示设置隐私模式下对应url的cookies,false表示设置正常非隐私模式下对应url的cookies。
默认值:false。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('configCookieSync')
.onClick(() => {
try {
// configCookieSync每次仅支持设置单个cookie值。
webview.WebCookieManager.configCookieSync('https://www.example.com', 'a=b');
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## configCookieSync14+
static configCookieSync(url: string, value: string, incognito: boolean, includeHttpOnly: boolean): void
为指定url设置cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要设置的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
| incognito | boolean | 是 | true表示设置隐私模式下对应url的cookies,false表示设置正常非隐私模式下对应url的cookies。 |
| includeHttpOnly | boolean | 是 | true表示允许覆盖含有http-only的cookies,false表示不允许覆盖含有http-only的cookies。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('configCookieSync')
.onClick(() => {
try {
// 仅支持设置单个cookie值。
webview.WebCookieManager.configCookieSync('https://www.example.com', 'a=b', false, false);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## configCookie11+
static configCookie(url: string, value: string, callback: AsyncCallback\): void
异步callback方式为指定url设置单个cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
| callback | AsyncCallback\ | 是 | callback回调,用于获取设置cookie的结果 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('configCookie')
.onClick(() => {
try {
webview.WebCookieManager.configCookie('https://www.example.com', "a=b", (error) => {
if (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## configCookie11+
static configCookie(url: string, value: string): Promise\
以异步Promise方式为指定url设置单个cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
**返回值:**
| 类型 | 说明 |
| ------ | ------------------------- |
| Promise\ | Promise实例,用于获取指定url设置单个cookie值是否成功。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('configCookie')
.onClick(() => {
try {
webview.WebCookieManager.configCookie('https://www.example.com', 'a=b')
.then(() => {
console.info('configCookie success!');
})
.catch((error: BusinessError) => {
console.info('error: ' + JSON.stringify(error));
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## configCookie14+
static configCookie(url: string, value: string, incognito: boolean, includeHttpOnly: boolean): Promise\
以异步Promise方式为指定url设置单个cookie的值。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
| incognito | boolean | 是 | true表示设置隐私模式下对应url的cookies,false表示设置正常非隐私模式下对应url的cookies。 |
| includeHttpOnly | boolean | 是 | true表示允许覆盖含有http-only的cookies,false表示不允许覆盖含有http-only的cookies。 |
**返回值:**
| 类型 | 说明 |
| ------ | ------------------------- |
| Promise\ | Promise实例,用于获取指定url设置单个cookie值是否成功。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('configCookie')
.onClick(() => {
try {
webview.WebCookieManager.configCookie('https://www.example.com', 'a=b', false, false)
.then(() => {
console.info('configCookie success!');
})
.catch((error: BusinessError) => {
console.info('error: ' + JSON.stringify(error));
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## saveCookieSync15+
static saveCookieSync(): void
将当前存在内存中的cookie同步保存到磁盘中。
**系统能力:** SystemCapability.Web.Webview.Core
> **说明:**
>
> saveCookieSync用于强制将需要持久化的cookies写入磁盘。PC/2in1和Tablet设备不会持久化session cookie,即使调用saveCookieSync,也不会将session cookie写入磁盘。
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('saveCookieSync')
.onClick(() => {
try {
webview.WebCookieManager.saveCookieSync();
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## saveCookieAsync
static saveCookieAsync(callback: AsyncCallback\): void
将当前存在内存中的cookie异步保存到磁盘中。
> **说明:**
>
> Cookie信息存储在应用沙箱路径下/proc/{pid}/root/data/storage/el2/base/cache/web/Cookies。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | :------------------------------------------------- |
| callback | AsyncCallback\ | 是 | callback回调,用于获取cookie是否成功保存。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('saveCookieAsync')
.onClick(() => {
try {
webview.WebCookieManager.saveCookieAsync((error) => {
if (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## saveCookieAsync
static saveCookieAsync(): Promise\
将当前存在内存中的cookie以Promise方法异步保存到磁盘中。
**系统能力:** SystemCapability.Web.Webview.Core
**返回值:**
| 类型 | 说明 |
| ---------------- | ----------------------------------------- |
| Promise\ | Promise实例,用于获取cookie是否成功保存。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('saveCookieAsync')
.onClick(() => {
try {
webview.WebCookieManager.saveCookieAsync()
.then(() => {
console.info("saveCookieAsyncCallback success!");
})
.catch((error: BusinessError) => {
console.error("error: " + error);
});
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## putAcceptCookieEnabled
static putAcceptCookieEnabled(accept: boolean): void
设置WebCookieManager实例是否拥有发送和接收cookie的权限。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | :----------------------------------- |
| accept | boolean | 是 | 设置是否拥有发送和接收cookie的权限,默认为true,表示拥有发送和接收cookie的权限。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('putAcceptCookieEnabled')
.onClick(() => {
try {
webview.WebCookieManager.putAcceptCookieEnabled(false);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## isCookieAllowed
static isCookieAllowed(): boolean
获取WebCookieManager实例是否拥有发送和接收cookie的权限。
**系统能力:** SystemCapability.Web.Webview.Core
**返回值:**
| 类型 | 说明 |
| ------- | -------------------------------- |
| boolean | 是否拥有发送和接收cookie的权限。
true表示拥有发送和接收cookie的权限,false表示无发送和接收cookie的权限。
默认值:true。 |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('isCookieAllowed')
.onClick(() => {
let result = webview.WebCookieManager.isCookieAllowed();
console.info("result: " + result);
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## putAcceptThirdPartyCookieEnabled
static putAcceptThirdPartyCookieEnabled(accept: boolean): void
设置WebCookieManager实例是否拥有发送和接收第三方cookie的权限。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | :----------------------------------------- |
| accept | boolean | 是 | 是否允许设置、获取第三方cookie。
true表示允许设置、获取第三方cookie,false表示不允许设置、获取第三方cookie。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('putAcceptThirdPartyCookieEnabled')
.onClick(() => {
try {
webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(false);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## isThirdPartyCookieAllowed
static isThirdPartyCookieAllowed(): boolean
获取WebCookieManager实例是否拥有发送和接收第三方cookie的权限。
**系统能力:** SystemCapability.Web.Webview.Core
**返回值:**
| 类型 | 说明 |
| ------- | -------------------------------------- |
| boolean | 是否拥有发送和接收第三方cookie的权限。
true表示拥有发送和接收第三方cookie的权限,false表示无发送和接收第三方cookie的权限。
默认值:false。 |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('isThirdPartyCookieAllowed')
.onClick(() => {
let result = webview.WebCookieManager.isThirdPartyCookieAllowed();
console.info("result: " + result);
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## existCookie
static existCookie(incognito?: boolean): boolean
获取是否存在cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | :----------------------------------------- |
| incognito11+ | boolean | 否 | true表示隐私模式下查询是否存在cookies,false表示正常非隐私模式下查询是否存在cookies。
默认值:false。 |
**返回值:**
| 类型 | 说明 |
| ------- | -------------------------------------- |
| boolean | true表示存在cookie,false表示不存在cookie。 |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('existCookie')
.onClick(() => {
let result = webview.WebCookieManager.existCookie();
console.info("result: " + result);
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## clearAllCookiesSync11+
static clearAllCookiesSync(incognito?: boolean): void
清除所有cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | :----------------------------------------- |
| incognito | boolean | 否 | true表示清除隐私模式下Webview的所有内存cookies,false表示清除正常非隐私模式下的持久化cookies。
默认值:false。 |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('clearAllCookiesSync')
.onClick(() => {
webview.WebCookieManager.clearAllCookiesSync();
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## clearAllCookies11+
static clearAllCookies(callback: AsyncCallback\): void
异步callback方式清除所有cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | :------------------------------------------------- |
| callback | AsyncCallback\ | 是 | callback回调,用于获取清除所有cookie是否成功。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('clearAllCookies')
.onClick(() => {
try {
webview.WebCookieManager.clearAllCookies((error) => {
if (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## clearAllCookies11+
static clearAllCookies(): Promise\
异步promise方式清除所有cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**返回值:**
| 类型 | 说明 |
| ---------------- | ----------------------------------------- |
| Promise\ | Promise实例,用于获取清除所有cookie是否成功。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('clearAllCookies')
.onClick(() => {
webview.WebCookieManager.clearAllCookies()
.then(() => {
console.log("clearAllCookies success!");
})
.catch((error: BusinessError) => {
console.error("error: " + error);
});
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## clearSessionCookieSync11+
static clearSessionCookieSync(): void
清除所有会话cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('clearSessionCookieSync')
.onClick(() => {
webview.WebCookieManager.clearSessionCookieSync();
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## clearSessionCookie11+
static clearSessionCookie(callback: AsyncCallback\): void
异步callback方式清除所有会话cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | :------------------------------------------------- |
| callback | AsyncCallback\ | 是 | callback回调,用于获取清除所有会话cookie是否成功。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('clearSessionCookie')
.onClick(() => {
try {
webview.WebCookieManager.clearSessionCookie((error) => {
if (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## clearSessionCookie11+
static clearSessionCookie(): Promise\
异步promise方式清除所有会话cookie。
**系统能力:** SystemCapability.Web.Webview.Core
**返回值:**
| 类型 | 说明 |
| ---------------- | ----------------------------------------- |
| Promise\ | Promise实例,用于获取清除所有会话cookie是否成功。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('clearSessionCookie')
.onClick(() => {
try {
webview.WebCookieManager.clearSessionCookie()
.then(() => {
console.info("clearSessionCookie success!");
})
.catch((error: BusinessError) => {
console.error("error: " + error);
});
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## getCookie(deprecated)
static getCookie(url: string): string
获取指定url对应cookie的值。
> **说明:**
>
> 从API version 9开始支持,从API version 11开始废弃。建议使用[fetchCookieSync](#fetchcookiesync11)替代
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
**返回值:**
| 类型 | 说明 |
| ------ | ------------------------- |
| string | 指定url对应的cookie的值。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('getCookie')
.onClick(() => {
try {
let value = webview.WebCookieManager.getCookie('https://www.example.com');
console.info("value: " + value);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## setCookie(deprecated)
static setCookie(url: string, value: string): void
为指定url设置单个cookie的值。
> **说明:**
>
> 从API version 9开始支持,从API version 11开始废弃。建议使用[configCookieSync11+](#configcookiesync11)替代
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要设置的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------ |
| 17100002 | URL error. No valid cookie found for the specified URL. |
| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('setCookie')
.onClick(() => {
try {
webview.WebCookieManager.setCookie('https://www.example.com', 'a=b');
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## deleteEntireCookie(deprecated)
static deleteEntireCookie(): void
清除所有cookie。
> **说明:**
>
> 从API version 9开始支持,从API version 11开始废弃。建议使用[clearAllCookiesSync](#clearallcookiessync11)替代
**系统能力:** SystemCapability.Web.Webview.Core
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('deleteEntireCookie')
.onClick(() => {
webview.WebCookieManager.deleteEntireCookie();
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```
## deleteSessionCookie(deprecated)
static deleteSessionCookie(): void
清除所有会话cookie。
> **说明:**
>
> 从API version 9开始支持,从API version 11开始废弃。建议使用[clearSessionCookieSync](#clearsessioncookiesync11)替代
**系统能力:** SystemCapability.Web.Webview.Core
**示例:**
```ts
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('deleteSessionCookie')
.onClick(() => {
webview.WebCookieManager.deleteSessionCookie();
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
```