# Class (WebCookieManager) Implements a **WebCookieManager** instance to manage behavior of cookies in **Web** components. All **Web** components in an application share a **WebCookieManager** instance. > **NOTE** > > - The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. > > - The initial APIs of this class are supported since API version 9. > > - You can preview how this component looks on a real device, but not in DevEco Studio Previewer. > > - Static methods must be used on the user interface (UI) thread. ## Modules to Import ```ts import { webview } from '@kit.ArkWeb'; ``` ## fetchCookieSync11+ static fetchCookieSync(url: string, incognito?: boolean): string Obtains the cookie value of the specified URL. > **NOTE** > > The system automatically deletes expired cookies. For data with the same key name, the new data overwrites the previous data. > > To obtain the cookie value that can be used, pass a complete link to **fetchCookieSync()**. > > **fetchCookieSync()** is used to obtain all cookie values. Cookie values are separated by semicolons (;). However, a specific cookie value cannot be obtained separately. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to obtain. A complete URL is recommended.| | incognito | boolean | No | Whether to obtain the cookie in incognito mode. The value **true** means to obtain the cookie in incognito mode, and **false** means the opposite.| **Return value** | Type | Description | | ------ | ------------------------- | | string | Cookie value corresponding to the specified URL.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log("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 Obtains the cookie value of a specified URL. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to obtain. A complete URL is recommended.| | callback | AsyncCallback\ | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log('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\ Obtains the cookie value of a specified URL. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to obtain. A complete URL is recommended.| **Return value** | Type | Description | | ------ | ------------------------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log("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\ Obtains the cookie value of a specified URL. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to obtain. A complete URL is recommended.| | incognito | boolean | Yes | Whether to obtain the cookie in incognito mode. The value **true** means to obtain the cookie in incognito mode, and **false** means the opposite.| **Return value** | Type | Description | | ------ | ------------------------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log("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 Sets a cookie for the specified URL. > **NOTE** > > You can set **url** in **configCookieSync** to a domain name so that the cookie is attached to the requests on the page. > > It is recommended that cookie syncing be completed before the **Web** component is loaded. > > If **configCookieSync()** is used to set cookies for two or more times, the cookies set each time are separated by semicolons (;). > > Cookies are periodically saved to the disk every 30s. You can also use the [saveCookieAsync](#savecookieasync) API to forcibly save cookies to the disk. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to set. A complete URL is recommended.| | value | string | Yes | Cookie value to set. | | incognito | boolean | No | Whether to set the cookies in incognito mode. The value **true** means to set the cookies in incognito mode, and **false** means the opposite.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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 { // Only one cookie value can be set in configCookieSync at a time. 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 Sets a cookie value for a specified URL. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to set. A complete URL is recommended.| | value | string | Yes | Cookie value to set. | | incognito | boolean | Yes | Whether to set the cookies in incognito mode. The value **true** means to set the cookies in incognito mode, and **false** means the opposite.| | includeHttpOnly | boolean | Yes | Whether to overwrite cookies containing **HttpOnly**. The value **true** means to overwrite cookies containing **HttpOnly**, and **false** means the opposite.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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 { // Only a single cookie value can be set. 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 Sets the value of a single cookie for a specified URL. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to set. A complete URL is recommended.| | value | string | Yes | Cookie value to set. | | callback | AsyncCallback\ | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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\ Sets the value of a single cookie for a specified URL. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to set. A complete URL is recommended.| | value | string | Yes | Cookie value to set. | **Return value** | Type | Description | | ------ | ------------------------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log('configCookie success!'); }) .catch((error: BusinessError) => { console.log('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\ Sets the value of a single cookie for a specified URL. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to set. A complete URL is recommended.| | value | string | Yes | Cookie value to set. | | incognito | boolean | Yes | Whether to set the cookies in incognito mode. The value **true** means to set the cookies in incognito mode, and **false** means the opposite.| | includeHttpOnly | boolean | Yes | Whether to overwrite cookies containing **HttpOnly**. The value **true** means to overwrite cookies containing **HttpOnly**, and **false** means the opposite.| **Return value** | Type | Description | | ------ | ------------------------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log('configCookie success!'); }) .catch((error: BusinessError) => { console.log('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 Saves the cookies in the memory to the drive. This API uses a synchronous callback to return the result. **System capability**: SystemCapability.Web.Webview.Core > **NOTE** > > **saveCookieSync** is used to forcibly write cookies that need to be persisted to disks. Session cookies are not persisted on PCs, 2-in-1 devices, or tablets, even if **saveCookieSync** is invoked. **Example** ```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 Saves the cookies in the memory to the drive. This API uses an asynchronous callback to return the result. > **NOTE** > > Cookie information is stored in the application sandbox path **/proc/{pid}/root/data/storage/el2/base/cache/web/Cookies**. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------- | ---- | :------------------------------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return whether the cookies are successfully saved.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | **Example** ```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\ Saves the cookies in the memory to the drive. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Return value** | Type | Description | | ---------------- | ----------------------------------------- | | Promise\ | Promise used to return the operation result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | **Example** ```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.log("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 Sets whether the **WebCookieManager** instance has the permission to send and receive cookies. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | :----------------------------------- | | accept | boolean | Yes | Whether the **WebCookieManager** instance has the permission to send and receive cookies. The default value is **true**, indicating that the **WebCookieManager** instance has the permission to send and receive cookies.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | **Example** ```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 Checks whether the **WebCookieManager** instance has the permission to send and receive cookies. **System capability**: SystemCapability.Web.Webview.Core **Return value** | Type | Description | | ------- | -------------------------------- | | boolean | Whether the **WebCookieManager** instance has the permission to send and receive cookies.
The value **true** indicates that the **WebCookieManager** instance has the permission to send and receive cookies, and **false** indicates the opposite.
Default value: **true**.| **Example** ```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.log("result: " + result); }) Web({ src: 'www.example.com', controller: this.controller }) } } } ``` ## putAcceptThirdPartyCookieEnabled static putAcceptThirdPartyCookieEnabled(accept: boolean): void Sets whether the **WebCookieManager** instance has the permission to send and receive third-party cookies. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | :----------------------------------------- | | accept | boolean | Yes | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.
The value **true** indicates that the **WebCookieManager** instance has the permission to send and receive third-party cookies, and **false** indicates the opposite.
The default value is **false**.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | **Example** ```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 Checks whether the **WebCookieManager** instance has the permission to send and receive third-party cookies. **System capability**: SystemCapability.Web.Webview.Core **Return value** | Type | Description | | ------- | -------------------------------------- | | boolean | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.
The value **true** indicates that the **WebCookieManager** instance has the permission to send and receive third-party cookies, and **false** indicates the opposite.
The default value is **false**.| **Example** ```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.log("result: " + result); }) Web({ src: 'www.example.com', controller: this.controller }) } } } ``` ## existCookie static existCookie(incognito?: boolean): boolean Checks whether cookies exist. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | :----------------------------------------- | | incognito11+ | boolean | No | Whether to check for cookies in incognito mode. The value **true** means to check for cookies in incognito mode, and **false** means the opposite.| **Return value** | Type | Description | | ------- | -------------------------------------- | | boolean | Whether cookies exist. The value **true** means that cookies exist, and **false** means the opposite.| **Example** ```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.log("result: " + result); }) Web({ src: 'www.example.com', controller: this.controller }) } } } ``` ## clearAllCookiesSync11+ static clearAllCookiesSync(incognito?: boolean): void Deletes all cookies. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------- | ---- | :----------------------------------------- | | incognito | boolean | No | Whether to clear all cookies in incognito mode. The value **true** means to clear all cookies in incognito mode, and **false** means the opposite.| **Example** ```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 Clears all cookies. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------- | ---- | :------------------------------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | **Example** ```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\ Clears all cookies. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Return value** | Type | Description | | ---------------- | ----------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. | **Example** ```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() .then(() => { console.log("clearAllCookies 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 }) } } } ``` ## clearSessionCookieSync11+ static clearSessionCookieSync(): void Deletes all session cookies. **System capability**: SystemCapability.Web.Webview.Core **Example** ```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 Clears all session cookies. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------- | ---- | :------------------------------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | **Example** ```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\ Clears all session cookies. This API uses a promise to return the result. **System capability**: SystemCapability.Web.Webview.Core **Return value** | Type | Description | | ---------------- | ----------------------------------------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. | **Example** ```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.log("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 Obtains the cookie value of the specified URL. > **NOTE** > > This API is supported since API version 9 and deprecated since API version 11. You are advised to use [fetchCookieSync](#fetchcookiesync11) instead. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to obtain. A complete URL is recommended.| **Return value** | Type | Description | | ------ | ------------------------- | | string | Cookie value corresponding to the specified URL.| **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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.log("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 Sets a cookie for the specified URL. > **NOTE** > > This API is supported since API version 9 and deprecated since API version 11. You are advised to use [configCookieSync11+](#configcookiesync11) instead. **System capability**: SystemCapability.Web.Webview.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | :------------------------ | | url | string | Yes | URL of the cookie to set. A complete URL is recommended.| | value | string | Yes | Cookie value to set. | **Error codes** For details about the error codes, see [Webview Error Codes](errorcode-webview.md). | ID| Error Message | | -------- | ------------------------------------------------------ | | 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. | **Example** ```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 Deletes all cookies. > **NOTE** > > This API is supported since API version 9 and deprecated since API version 11. You are advised to use [clearAllCookiesSync](#clearallcookiessync11) instead. **System capability**: SystemCapability.Web.Webview.Core **Example** ```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 Deletes all session cookies. > **NOTE** > > This API is supported since API version 9 and deprecated since API version 11. You are advised to use [clearSessionCookieSync](#clearsessioncookiesync11) instead. **System capability**: SystemCapability.Web.Webview.Core **Example** ```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 }) } } } ```