• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebCookieManager)
2
3Implements a **WebCookieManager** instance to manage behavior of cookies in **Web** components. All **Web** components in an application share a **WebCookieManager** instance.
4
5> **NOTE**
6>
7> - 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.
8>
9> - The initial APIs of this class are supported since API version 9.
10>
11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer.
12>
13> - Static methods must be used on the user interface (UI) thread.
14
15## Modules to Import
16
17```ts
18import { webview } from '@kit.ArkWeb';
19```
20
21## fetchCookieSync<sup>11+</sup>
22
23static fetchCookieSync(url: string, incognito?: boolean): string
24
25Obtains the cookie value of the specified URL.
26
27> **NOTE**
28>
29> The system automatically deletes expired cookies. For data with the same key name, the new data overwrites the previous data.
30>
31> To obtain the cookie value that can be used, pass a complete link to **fetchCookieSync()**.
32>
33> **fetchCookieSync()** is used to obtain all cookie values. Cookie values are separated by semicolons (;). However, a specific cookie value cannot be obtained separately.
34
35**System capability**: SystemCapability.Web.Webview.Core
36
37**Parameters**
38
39| Name| Type  | Mandatory| Description                     |
40| ------ | ------ | ---- | :------------------------ |
41| url    | string | Yes  | URL of the cookie to obtain. A complete URL is recommended.|
42| 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.|
43
44**Return value**
45
46| Type  | Description                     |
47| ------ | ------------------------- |
48| string | Cookie value corresponding to the specified URL.|
49
50**Error codes**
51
52For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
53
54| ID| Error Message                                              |
55| -------- | ------------------------------------------------------ |
56| 17100002 | URL error. No valid cookie found for the specified URL. |
57| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
58
59**Example**
60
61```ts
62// xxx.ets
63import { webview } from '@kit.ArkWeb';
64import { BusinessError } from '@kit.BasicServicesKit';
65
66@Entry
67@Component
68struct WebComponent {
69  controller: webview.WebviewController = new webview.WebviewController();
70
71  build() {
72    Column() {
73      Button('fetchCookieSync')
74        .onClick(() => {
75          try {
76            let value = webview.WebCookieManager.fetchCookieSync('https://www.example.com');
77            console.log("fetchCookieSync cookie = " + value);
78          } catch (error) {
79            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
80          }
81        })
82      Web({ src: 'www.example.com', controller: this.controller })
83    }
84  }
85}
86```
87
88## fetchCookie<sup>11+</sup>
89
90static fetchCookie(url: string, callback: AsyncCallback\<string>): void
91
92Obtains the cookie value of a specified URL. This API uses an asynchronous callback to return the result.
93
94**System capability**: SystemCapability.Web.Webview.Core
95
96**Parameters**
97
98| Name| Type  | Mandatory| Description                     |
99| ------ | ------ | ---- | :------------------------ |
100| url    | string | Yes  | URL of the cookie to obtain. A complete URL is recommended.|
101| callback | AsyncCallback\<string> | Yes| Callback used to return the result.|
102
103**Error codes**
104
105For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
106
107| ID| Error Message                                              |
108| -------- | ------------------------------------------------------ |
109| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
110| 17100002 | URL error. No valid cookie found for the specified URL.   |
111
112**Example**
113
114```ts
115// xxx.ets
116import { webview } from '@kit.ArkWeb';
117import { BusinessError } from '@kit.BasicServicesKit';
118
119@Entry
120@Component
121struct WebComponent {
122  controller: webview.WebviewController = new webview.WebviewController();
123
124  build() {
125    Column() {
126      Button('fetchCookie')
127        .onClick(() => {
128          try {
129            webview.WebCookieManager.fetchCookie('https://www.example.com', (error, cookie) => {
130              if (error) {
131                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
132                return;
133              }
134              if (cookie) {
135                console.log('fetchCookie cookie = ' + cookie);
136              }
137            })
138          } catch (error) {
139            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
140          }
141        })
142      Web({ src: 'www.example.com', controller: this.controller })
143    }
144  }
145}
146```
147
148## fetchCookie<sup>11+</sup>
149
150static fetchCookie(url: string): Promise\<string>
151
152Obtains the cookie value of a specified URL. This API uses a promise to return the result.
153
154**System capability**: SystemCapability.Web.Webview.Core
155
156**Parameters**
157
158| Name| Type  | Mandatory| Description                     |
159| ------ | ------ | ---- | :------------------------ |
160| url    | string | Yes  | URL of the cookie to obtain. A complete URL is recommended.|
161
162**Return value**
163
164| Type  | Description                     |
165| ------ | ------------------------- |
166| Promise\<string> | Promise used to return the result.|
167
168**Error codes**
169
170For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
171
172| ID| Error Message                                              |
173| -------- | ------------------------------------------------------ |
174| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
175| 17100002 | URL error. No valid cookie found for the specified URL.  |
176
177**Example**
178
179```ts
180// xxx.ets
181import { webview } from '@kit.ArkWeb';
182import { BusinessError } from '@kit.BasicServicesKit';
183
184@Entry
185@Component
186struct WebComponent {
187  controller: webview.WebviewController = new webview.WebviewController();
188
189  build() {
190    Column() {
191      Button('fetchCookie')
192        .onClick(() => {
193          try {
194            webview.WebCookieManager.fetchCookie('https://www.example.com')
195              .then(cookie => {
196                console.log("fetchCookie cookie = " + cookie);
197              })
198              .catch((error: BusinessError) => {
199                console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
200              })
201          } catch (error) {
202            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
203          }
204        })
205      Web({ src: 'www.example.com', controller: this.controller })
206    }
207  }
208}
209```
210
211## fetchCookie<sup>14+</sup>
212
213static fetchCookie(url: string, incognito: boolean): Promise\<string>
214
215Obtains the cookie value of a specified URL. This API uses a promise to return the result.
216
217**System capability**: SystemCapability.Web.Webview.Core
218
219**Parameters**
220
221| Name| Type  | Mandatory| Description                     |
222| ------ | ------ | ---- | :------------------------ |
223| url    | string | Yes  | URL of the cookie to obtain. A complete URL is recommended.|
224| 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.|
225
226**Return value**
227
228| Type  | Description                     |
229| ------ | ------------------------- |
230| Promise\<string> | Promise used to return the result.|
231
232**Error codes**
233
234For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
235
236| ID| Error Message                                              |
237| -------- | ------------------------------------------------------ |
238| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
239| 17100002 | URL error. No valid cookie found for the specified URL. |
240
241**Example**
242
243```ts
244// xxx.ets
245import { webview } from '@kit.ArkWeb';
246import { BusinessError } from '@kit.BasicServicesKit';
247
248@Entry
249@Component
250struct WebComponent {
251  controller: webview.WebviewController = new webview.WebviewController();
252
253  build() {
254    Column() {
255      Button('fetchCookie')
256        .onClick(() => {
257          try {
258            webview.WebCookieManager.fetchCookie('https://www.example.com', false)
259              .then(cookie => {
260                console.log("fetchCookie cookie = " + cookie);
261              })
262              .catch((error: BusinessError) => {
263                console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
264              })
265          } catch (error) {
266            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
267          }
268        })
269      Web({ src: 'www.example.com', controller: this.controller })
270    }
271  }
272}
273```
274
275## configCookieSync<sup>11+</sup>
276
277static configCookieSync(url: string, value: string, incognito?: boolean): void
278
279Sets a cookie for the specified URL.
280
281> **NOTE**
282>
283> You can set **url** in **configCookieSync** to a domain name so that the cookie is attached to the requests on the page.
284>
285> It is recommended that cookie syncing be completed before the **Web** component is loaded.
286>
287> If **configCookieSync()** is used to set cookies for two or more times, the cookies set each time are separated by semicolons (;).
288>
289> Cookies are periodically saved to the disk every 30s. You can also use the [saveCookieAsync](#savecookieasync) API to forcibly save cookies to the disk.
290
291**System capability**: SystemCapability.Web.Webview.Core
292
293**Parameters**
294
295| Name| Type  | Mandatory| Description                     |
296| ------ | ------ | ---- | :------------------------ |
297| url    | string | Yes  | URL of the cookie to set. A complete URL is recommended.|
298| value  | string | Yes  | Cookie value to set.     |
299| 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.|
300
301**Error codes**
302
303For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
304
305| ID| Error Message                                              |
306| -------- | ------------------------------------------------------ |
307| 17100002 | URL error. No valid cookie found for the specified URL.  |
308| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
309| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
310
311**Example**
312
313```ts
314// xxx.ets
315import { webview } from '@kit.ArkWeb';
316import { BusinessError } from '@kit.BasicServicesKit';
317
318@Entry
319@Component
320struct WebComponent {
321  controller: webview.WebviewController = new webview.WebviewController();
322
323  build() {
324    Column() {
325      Button('configCookieSync')
326        .onClick(() => {
327          try {
328            // Only one cookie value can be set in configCookieSync at a time.
329            webview.WebCookieManager.configCookieSync('https://www.example.com', 'a=b');
330          } catch (error) {
331            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
332          }
333        })
334      Web({ src: 'www.example.com', controller: this.controller })
335    }
336  }
337}
338```
339
340## configCookieSync<sup>14+</sup>
341
342static configCookieSync(url: string, value: string, incognito: boolean, includeHttpOnly: boolean): void
343
344Sets a cookie value for a specified URL.
345
346**System capability**: SystemCapability.Web.Webview.Core
347
348**Parameters**
349
350| Name| Type  | Mandatory| Description                     |
351| ------ | ------ | ---- | :------------------------ |
352| url    | string | Yes  | URL of the cookie to set. A complete URL is recommended.|
353| value  | string | Yes  | Cookie value to set.     |
354| 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.|
355| includeHttpOnly    | boolean | Yes  | Whether to overwrite cookies containing **HttpOnly**. The value **true** means to overwrite cookies containing **HttpOnly**, and **false** means the opposite.|
356
357**Error codes**
358
359For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
360
361| ID| Error Message                                              |
362| -------- | ------------------------------------------------------ |
363| 17100002 | URL error. No valid cookie found for the specified URL.  |
364| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
365| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
366
367**Example**
368
369```ts
370// xxx.ets
371import { webview } from '@kit.ArkWeb';
372import { BusinessError } from '@kit.BasicServicesKit';
373
374@Entry
375@Component
376struct WebComponent {
377  controller: webview.WebviewController = new webview.WebviewController();
378
379  build() {
380    Column() {
381      Button('configCookieSync')
382        .onClick(() => {
383          try {
384            // Only a single cookie value can be set.
385            webview.WebCookieManager.configCookieSync('https://www.example.com', 'a=b', false, false);
386          } catch (error) {
387            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
388          }
389        })
390      Web({ src: 'www.example.com', controller: this.controller })
391    }
392  }
393}
394```
395
396## configCookie<sup>11+</sup>
397
398static configCookie(url: string, value: string, callback: AsyncCallback\<void>): void
399
400Sets the value of a single cookie for a specified URL. This API uses an asynchronous callback to return the result.
401
402**System capability**: SystemCapability.Web.Webview.Core
403
404**Parameters**
405
406| Name| Type  | Mandatory| Description                     |
407| ------ | ------ | ---- | :------------------------ |
408| url    | string | Yes  | URL of the cookie to set. A complete URL is recommended.|
409| value  | string | Yes  | Cookie value to set.     |
410| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
411
412**Error codes**
413
414For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
415
416| ID| Error Message                                              |
417| -------- | ------------------------------------------------------ |
418| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
419| 17100002 | URL error. No valid cookie found for the specified URL.  |
420| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
421
422**Example**
423
424```ts
425// xxx.ets
426import { webview } from '@kit.ArkWeb';
427import { BusinessError } from '@kit.BasicServicesKit';
428
429@Entry
430@Component
431struct WebComponent {
432  controller: webview.WebviewController = new webview.WebviewController();
433
434  build() {
435    Column() {
436      Button('configCookie')
437        .onClick(() => {
438          try {
439            webview.WebCookieManager.configCookie('https://www.example.com', "a=b", (error) => {
440              if (error) {
441                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
442              }
443            })
444          } catch (error) {
445            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
446          }
447        })
448      Web({ src: 'www.example.com', controller: this.controller })
449    }
450  }
451}
452```
453
454## configCookie<sup>11+</sup>
455
456static configCookie(url: string, value: string): Promise\<void>
457
458Sets the value of a single cookie for a specified URL. This API uses a promise to return the result.
459
460**System capability**: SystemCapability.Web.Webview.Core
461
462**Parameters**
463
464| Name| Type  | Mandatory| Description                     |
465| ------ | ------ | ---- | :------------------------ |
466| url    | string | Yes  | URL of the cookie to set. A complete URL is recommended.|
467| value  | string | Yes  | Cookie value to set.     |
468
469**Return value**
470
471| Type  | Description                     |
472| ------ | ------------------------- |
473| Promise\<void> | Promise used to return the result.|
474
475**Error codes**
476
477For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
478
479| ID| Error Message                                               |
480| -------- | ------------------------------------------------------ |
481| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
482| 17100002 | URL error. No valid cookie found for the specified URL. |
483| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
484
485**Example**
486
487```ts
488// xxx.ets
489import { webview } from '@kit.ArkWeb';
490import { BusinessError } from '@kit.BasicServicesKit';
491
492@Entry
493@Component
494struct WebComponent {
495  controller: webview.WebviewController = new webview.WebviewController();
496
497  build() {
498    Column() {
499      Button('configCookie')
500        .onClick(() => {
501          try {
502            webview.WebCookieManager.configCookie('https://www.example.com', 'a=b')
503              .then(() => {
504                console.log('configCookie success!');
505              })
506              .catch((error: BusinessError) => {
507                console.log('error: ' + JSON.stringify(error));
508              })
509          } catch (error) {
510            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
511          }
512        })
513      Web({ src: 'www.example.com', controller: this.controller })
514    }
515  }
516}
517```
518
519## configCookie<sup>14+</sup>
520
521static configCookie(url: string, value: string, incognito: boolean, includeHttpOnly: boolean): Promise\<void>
522
523Sets the value of a single cookie for a specified URL. This API uses a promise to return the result.
524
525**System capability**: SystemCapability.Web.Webview.Core
526
527**Parameters**
528
529| Name| Type  | Mandatory| Description                     |
530| ------ | ------ | ---- | :------------------------ |
531| url    | string | Yes  | URL of the cookie to set. A complete URL is recommended.|
532| value  | string | Yes  | Cookie value to set.     |
533| 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.|
534| includeHttpOnly    | boolean | Yes  | Whether to overwrite cookies containing **HttpOnly**. The value **true** means to overwrite cookies containing **HttpOnly**, and **false** means the opposite.|
535
536**Return value**
537
538| Type  | Description                     |
539| ------ | ------------------------- |
540| Promise\<void> | Promise used to return the result.|
541
542**Error codes**
543
544For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
545
546| ID| Error Message                                               |
547| -------- | ------------------------------------------------------ |
548| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
549| 17100002 | URL error. No valid cookie found for the specified URL. |
550| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
551
552**Example**
553
554```ts
555// xxx.ets
556import { webview } from '@kit.ArkWeb';
557import { BusinessError } from '@kit.BasicServicesKit';
558
559@Entry
560@Component
561struct WebComponent {
562  controller: webview.WebviewController = new webview.WebviewController();
563
564  build() {
565    Column() {
566      Button('configCookie')
567        .onClick(() => {
568          try {
569            webview.WebCookieManager.configCookie('https://www.example.com', 'a=b', false, false)
570              .then(() => {
571                console.log('configCookie success!');
572              })
573              .catch((error: BusinessError) => {
574                console.log('error: ' + JSON.stringify(error));
575              })
576          } catch (error) {
577            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
578          }
579        })
580      Web({ src: 'www.example.com', controller: this.controller })
581    }
582  }
583}
584```
585
586## saveCookieSync<sup>15+</sup>
587
588static saveCookieSync(): void
589
590Saves the cookies in the memory to the drive. This API uses a synchronous callback to return the result.
591
592**System capability**: SystemCapability.Web.Webview.Core
593
594> **NOTE**
595>
596> **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.
597
598**Example**
599
600```ts
601// xxx.ets
602import { webview } from '@kit.ArkWeb';
603import { BusinessError } from '@kit.BasicServicesKit';
604
605@Entry
606@Component
607struct WebComponent {
608  controller: webview.WebviewController = new webview.WebviewController();
609
610  build() {
611    Column() {
612      Button('saveCookieSync')
613        .onClick(() => {
614          try {
615            webview.WebCookieManager.saveCookieSync();
616          } catch (error) {
617            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
618          }
619        })
620      Web({ src: 'www.example.com', controller: this.controller })
621    }
622  }
623}
624```
625
626## saveCookieAsync
627
628static saveCookieAsync(callback: AsyncCallback\<void>): void
629
630Saves the cookies in the memory to the drive. This API uses an asynchronous callback to return the result.
631
632> **NOTE**
633>
634> Cookie information is stored in the application sandbox path **/proc/{pid}/root/data/storage/el2/base/cache/web/Cookies**.
635
636**System capability**: SystemCapability.Web.Webview.Core
637
638**Parameters**
639
640| Name  | Type                  | Mandatory| Description                                              |
641| -------- | ---------------------- | ---- | :------------------------------------------------- |
642| callback | AsyncCallback\<void> | Yes  | Callback used to return whether the cookies are successfully saved.|
643
644**Error codes**
645
646For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
647
648| ID| Error Message                                               |
649| -------- | ------------------------------------------------------ |
650| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
651
652**Example**
653
654```ts
655// xxx.ets
656import { webview } from '@kit.ArkWeb';
657import { BusinessError } from '@kit.BasicServicesKit';
658
659@Entry
660@Component
661struct WebComponent {
662  controller: webview.WebviewController = new webview.WebviewController();
663
664  build() {
665    Column() {
666      Button('saveCookieAsync')
667        .onClick(() => {
668          try {
669            webview.WebCookieManager.saveCookieAsync((error) => {
670              if (error) {
671                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
672              }
673            })
674          } catch (error) {
675            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
676          }
677        })
678      Web({ src: 'www.example.com', controller: this.controller })
679    }
680  }
681}
682```
683
684## saveCookieAsync
685
686static saveCookieAsync(): Promise\<void>
687
688Saves the cookies in the memory to the drive. This API uses a promise to return the result.
689
690**System capability**: SystemCapability.Web.Webview.Core
691
692**Return value**
693
694| Type            | Description                                     |
695| ---------------- | ----------------------------------------- |
696| Promise\<void> | Promise used to return the operation result.|
697
698**Error codes**
699
700For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
701
702| ID| Error Message                                               |
703| -------- | ------------------------------------------------------ |
704| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
705
706**Example**
707
708```ts
709// xxx.ets
710import { webview } from '@kit.ArkWeb';
711import { BusinessError } from '@kit.BasicServicesKit';
712
713@Entry
714@Component
715struct WebComponent {
716  controller: webview.WebviewController = new webview.WebviewController();
717
718  build() {
719    Column() {
720      Button('saveCookieAsync')
721        .onClick(() => {
722          try {
723            webview.WebCookieManager.saveCookieAsync()
724              .then(() => {
725                console.log("saveCookieAsyncCallback success!");
726              })
727              .catch((error: BusinessError) => {
728                console.error("error: " + error);
729              });
730          } catch (error) {
731            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
732          }
733        })
734      Web({ src: 'www.example.com', controller: this.controller })
735    }
736  }
737}
738```
739
740## putAcceptCookieEnabled
741
742static putAcceptCookieEnabled(accept: boolean): void
743
744Sets whether the **WebCookieManager** instance has the permission to send and receive cookies.
745
746**System capability**: SystemCapability.Web.Webview.Core
747
748**Parameters**
749
750| Name| Type   | Mandatory| Description                                |
751| ------ | ------- | ---- | :----------------------------------- |
752| 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.|
753
754**Error codes**
755
756For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
757
758| ID| Error Message                                               |
759| -------- | ------------------------------------------------------ |
760| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
761
762**Example**
763
764```ts
765// xxx.ets
766import { webview } from '@kit.ArkWeb';
767import { BusinessError } from '@kit.BasicServicesKit';
768
769@Entry
770@Component
771struct WebComponent {
772  controller: webview.WebviewController = new webview.WebviewController();
773
774  build() {
775    Column() {
776      Button('putAcceptCookieEnabled')
777        .onClick(() => {
778          try {
779            webview.WebCookieManager.putAcceptCookieEnabled(false);
780          } catch (error) {
781            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
782          }
783        })
784      Web({ src: 'www.example.com', controller: this.controller })
785    }
786  }
787}
788```
789
790## isCookieAllowed
791
792static isCookieAllowed(): boolean
793
794Checks whether the **WebCookieManager** instance has the permission to send and receive cookies.
795
796**System capability**: SystemCapability.Web.Webview.Core
797
798**Return value**
799
800| Type   | Description                            |
801| ------- | -------------------------------- |
802| boolean | Whether the **WebCookieManager** instance has the permission to send and receive cookies.<br>The value **true** indicates that the **WebCookieManager** instance has the permission to send and receive cookies, and **false** indicates the opposite.<br>Default value: **true**.|
803
804**Example**
805
806```ts
807// xxx.ets
808import { webview } from '@kit.ArkWeb';
809
810@Entry
811@Component
812struct WebComponent {
813  controller: webview.WebviewController = new webview.WebviewController();
814
815  build() {
816    Column() {
817      Button('isCookieAllowed')
818        .onClick(() => {
819          let result = webview.WebCookieManager.isCookieAllowed();
820          console.log("result: " + result);
821        })
822      Web({ src: 'www.example.com', controller: this.controller })
823    }
824  }
825}
826```
827
828## putAcceptThirdPartyCookieEnabled
829
830static putAcceptThirdPartyCookieEnabled(accept: boolean): void
831
832Sets whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.
833
834**System capability**: SystemCapability.Web.Webview.Core
835
836**Parameters**
837
838| Name| Type   | Mandatory| Description                                      |
839| ------ | ------- | ---- | :----------------------------------------- |
840| accept | boolean | Yes  | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.<br>The value **true** indicates that the **WebCookieManager** instance has the permission to send and receive third-party cookies, and **false** indicates the opposite.<br>The default value is **false**.|
841
842**Error codes**
843
844For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
845
846| ID| Error Message                                               |
847| -------- | ------------------------------------------------------ |
848| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
849
850**Example**
851
852```ts
853// xxx.ets
854import { webview } from '@kit.ArkWeb';
855import { BusinessError } from '@kit.BasicServicesKit';
856
857@Entry
858@Component
859struct WebComponent {
860  controller: webview.WebviewController = new webview.WebviewController();
861
862  build() {
863    Column() {
864      Button('putAcceptThirdPartyCookieEnabled')
865        .onClick(() => {
866          try {
867            webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(false);
868          } catch (error) {
869            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
870          }
871        })
872      Web({ src: 'www.example.com', controller: this.controller })
873    }
874  }
875}
876```
877
878## isThirdPartyCookieAllowed
879
880static isThirdPartyCookieAllowed(): boolean
881
882Checks whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.
883
884**System capability**: SystemCapability.Web.Webview.Core
885
886**Return value**
887
888| Type   | Description                                  |
889| ------- | -------------------------------------- |
890| boolean | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.<br>The value **true** indicates that the **WebCookieManager** instance has the permission to send and receive third-party cookies, and **false** indicates the opposite.<br>The default value is **false**.|
891
892**Example**
893
894```ts
895// xxx.ets
896import { webview } from '@kit.ArkWeb';
897
898@Entry
899@Component
900struct WebComponent {
901  controller: webview.WebviewController = new webview.WebviewController();
902
903  build() {
904    Column() {
905      Button('isThirdPartyCookieAllowed')
906        .onClick(() => {
907          let result = webview.WebCookieManager.isThirdPartyCookieAllowed();
908          console.log("result: " + result);
909        })
910      Web({ src: 'www.example.com', controller: this.controller })
911    }
912  }
913}
914```
915
916## existCookie
917
918static existCookie(incognito?: boolean): boolean
919
920Checks whether cookies exist.
921
922**System capability**: SystemCapability.Web.Webview.Core
923
924**Parameters**
925
926| Name| Type   | Mandatory| Description                                      |
927| ------ | ------- | ---- | :----------------------------------------- |
928| incognito<sup>11+</sup>    | 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.|
929
930**Return value**
931
932| Type   | Description                                  |
933| ------- | -------------------------------------- |
934| boolean | Whether cookies exist. The value **true** means that cookies exist, and **false** means the opposite.|
935
936**Example**
937
938```ts
939// xxx.ets
940import { webview } from '@kit.ArkWeb';
941
942@Entry
943@Component
944struct WebComponent {
945  controller: webview.WebviewController = new webview.WebviewController();
946
947  build() {
948    Column() {
949      Button('existCookie')
950        .onClick(() => {
951          let result = webview.WebCookieManager.existCookie();
952          console.log("result: " + result);
953        })
954      Web({ src: 'www.example.com', controller: this.controller })
955    }
956  }
957}
958```
959
960## clearAllCookiesSync<sup>11+</sup>
961
962static clearAllCookiesSync(incognito?: boolean): void
963
964Deletes all cookies.
965
966**System capability**: SystemCapability.Web.Webview.Core
967
968**Parameters**
969
970| Name| Type   | Mandatory| Description                                      |
971| ------ | ------- | ---- | :----------------------------------------- |
972| 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.|
973
974**Example**
975
976```ts
977// xxx.ets
978import { webview } from '@kit.ArkWeb';
979
980@Entry
981@Component
982struct WebComponent {
983  controller: webview.WebviewController = new webview.WebviewController();
984
985  build() {
986    Column() {
987      Button('clearAllCookiesSync')
988        .onClick(() => {
989          webview.WebCookieManager.clearAllCookiesSync();
990        })
991      Web({ src: 'www.example.com', controller: this.controller })
992    }
993  }
994}
995```
996
997## clearAllCookies<sup>11+</sup>
998
999static clearAllCookies(callback: AsyncCallback\<void>): void
1000
1001Clears all cookies. This API uses an asynchronous callback to return the result.
1002
1003**System capability**: SystemCapability.Web.Webview.Core
1004
1005**Parameters**
1006
1007| Name  | Type                  | Mandatory| Description                                              |
1008| -------- | ---------------------- | ---- | :------------------------------------------------- |
1009| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
1010
1011**Error codes**
1012
1013For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
1014
1015| ID| Error Message                                               |
1016| -------- | ------------------------------------------------------ |
1017| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
1018
1019**Example**
1020
1021```ts
1022// xxx.ets
1023import { webview } from '@kit.ArkWeb';
1024import { BusinessError } from '@kit.BasicServicesKit';
1025
1026@Entry
1027@Component
1028struct WebComponent {
1029  controller: webview.WebviewController = new webview.WebviewController();
1030
1031  build() {
1032    Column() {
1033      Button('clearAllCookies')
1034        .onClick(() => {
1035          try {
1036            webview.WebCookieManager.clearAllCookies((error) => {
1037              if (error) {
1038                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1039              }
1040            })
1041          } catch (error) {
1042            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1043          }
1044        })
1045      Web({ src: 'www.example.com', controller: this.controller })
1046    }
1047  }
1048}
1049```
1050
1051## clearAllCookies<sup>11+</sup>
1052
1053static clearAllCookies(): Promise\<void>
1054
1055Clears all cookies. This API uses a promise to return the result.
1056
1057**System capability**: SystemCapability.Web.Webview.Core
1058
1059**Return value**
1060
1061| Type            | Description                                     |
1062| ---------------- | ----------------------------------------- |
1063| Promise\<void> | Promise used to return the result.|
1064
1065**Error codes**
1066
1067For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
1068
1069| ID| Error Message                                               |
1070| -------- | ------------------------------------------------------ |
1071| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. |
1072
1073**Example**
1074
1075```ts
1076// xxx.ets
1077import { webview } from '@kit.ArkWeb';
1078import { BusinessError } from '@kit.BasicServicesKit';
1079
1080@Entry
1081@Component
1082struct WebComponent {
1083  controller: webview.WebviewController = new webview.WebviewController();
1084
1085  build() {
1086    Column() {
1087      Button('clearAllCookies')
1088        .onClick(() => {
1089          try {
1090            webview.WebCookieManager.clearAllCookies()
1091              .then(() => {
1092                console.log("clearAllCookies success!");
1093              })
1094              .catch((error: BusinessError) => {
1095                console.error("error: " + error);
1096              });
1097          } catch (error) {
1098            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1099          }
1100        })
1101      Web({ src: 'www.example.com', controller: this.controller })
1102    }
1103  }
1104}
1105```
1106
1107## clearSessionCookieSync<sup>11+</sup>
1108
1109static clearSessionCookieSync(): void
1110
1111Deletes all session cookies.
1112
1113**System capability**: SystemCapability.Web.Webview.Core
1114
1115**Example**
1116
1117```ts
1118// xxx.ets
1119import { webview } from '@kit.ArkWeb';
1120
1121@Entry
1122@Component
1123struct WebComponent {
1124  controller: webview.WebviewController = new webview.WebviewController();
1125
1126  build() {
1127    Column() {
1128      Button('clearSessionCookieSync')
1129        .onClick(() => {
1130          webview.WebCookieManager.clearSessionCookieSync();
1131        })
1132      Web({ src: 'www.example.com', controller: this.controller })
1133    }
1134  }
1135}
1136```
1137
1138## clearSessionCookie<sup>11+</sup>
1139
1140static clearSessionCookie(callback: AsyncCallback\<void>): void
1141
1142Clears all session cookies. This API uses an asynchronous callback to return the result.
1143
1144**System capability**: SystemCapability.Web.Webview.Core
1145
1146**Parameters**
1147
1148| Name  | Type                  | Mandatory| Description                                              |
1149| -------- | ---------------------- | ---- | :------------------------------------------------- |
1150| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
1151
1152**Error codes**
1153
1154For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
1155
1156| ID| Error Message                                               |
1157| -------- | ------------------------------------------------------ |
1158| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
1159
1160**Example**
1161
1162```ts
1163// xxx.ets
1164import { webview } from '@kit.ArkWeb';
1165import { BusinessError } from '@kit.BasicServicesKit';
1166
1167@Entry
1168@Component
1169struct WebComponent {
1170  controller: webview.WebviewController = new webview.WebviewController();
1171
1172  build() {
1173    Column() {
1174      Button('clearSessionCookie')
1175        .onClick(() => {
1176          try {
1177            webview.WebCookieManager.clearSessionCookie((error) => {
1178              if (error) {
1179                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1180              }
1181            })
1182          } catch (error) {
1183            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1184          }
1185        })
1186      Web({ src: 'www.example.com', controller: this.controller })
1187    }
1188  }
1189}
1190```
1191
1192## clearSessionCookie<sup>11+</sup>
1193
1194static clearSessionCookie(): Promise\<void>
1195
1196Clears all session cookies. This API uses a promise to return the result.
1197
1198**System capability**: SystemCapability.Web.Webview.Core
1199
1200**Return value**
1201
1202| Type            | Description                                     |
1203| ---------------- | ----------------------------------------- |
1204| Promise\<void> | Promise used to return the result.|
1205
1206**Error codes**
1207
1208For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
1209
1210| ID| Error Message                                               |
1211| -------- | ------------------------------------------------------ |
1212| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. |
1213
1214**Example**
1215
1216```ts
1217// xxx.ets
1218import { webview } from '@kit.ArkWeb';
1219import { BusinessError } from '@kit.BasicServicesKit';
1220
1221@Entry
1222@Component
1223struct WebComponent {
1224  controller: webview.WebviewController = new webview.WebviewController();
1225
1226  build() {
1227    Column() {
1228      Button('clearSessionCookie')
1229        .onClick(() => {
1230          try {
1231            webview.WebCookieManager.clearSessionCookie()
1232              .then(() => {
1233                console.log("clearSessionCookie success!");
1234              })
1235              .catch((error: BusinessError) => {
1236                console.error("error: " + error);
1237              });
1238          } catch (error) {
1239            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1240          }
1241        })
1242      Web({ src: 'www.example.com', controller: this.controller })
1243    }
1244  }
1245}
1246```
1247
1248## getCookie<sup>(deprecated)</sup>
1249
1250static getCookie(url: string): string
1251
1252Obtains the cookie value of the specified URL.
1253
1254> **NOTE**
1255>
1256> This API is supported since API version 9 and deprecated since API version 11. You are advised to use [fetchCookieSync](#fetchcookiesync11) instead.
1257
1258**System capability**: SystemCapability.Web.Webview.Core
1259
1260**Parameters**
1261
1262| Name| Type  | Mandatory| Description                     |
1263| ------ | ------ | ---- | :------------------------ |
1264| url    | string | Yes  | URL of the cookie to obtain. A complete URL is recommended.|
1265
1266**Return value**
1267
1268| Type  | Description                     |
1269| ------ | ------------------------- |
1270| string | Cookie value corresponding to the specified URL.|
1271
1272**Error codes**
1273
1274For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
1275
1276| ID| Error Message                                              |
1277| -------- | ------------------------------------------------------ |
1278| 17100002 | URL error. No valid cookie found for the specified URL.  |
1279| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
1280
1281**Example**
1282
1283```ts
1284// xxx.ets
1285import { webview } from '@kit.ArkWeb';
1286import { BusinessError } from '@kit.BasicServicesKit';
1287
1288@Entry
1289@Component
1290struct WebComponent {
1291  controller: webview.WebviewController = new webview.WebviewController();
1292
1293  build() {
1294    Column() {
1295      Button('getCookie')
1296        .onClick(() => {
1297          try {
1298            let value = webview.WebCookieManager.getCookie('https://www.example.com');
1299            console.log("value: " + value);
1300          } catch (error) {
1301            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1302          }
1303        })
1304      Web({ src: 'www.example.com', controller: this.controller })
1305    }
1306  }
1307}
1308```
1309
1310## setCookie<sup>(deprecated)</sup>
1311
1312static setCookie(url: string, value: string): void
1313
1314Sets a cookie for the specified URL.
1315
1316> **NOTE**
1317>
1318> This API is supported since API version 9 and deprecated since API version 11. You are advised to use [configCookieSync<sup>11+</sup>](#configcookiesync11) instead.
1319
1320**System capability**: SystemCapability.Web.Webview.Core
1321
1322**Parameters**
1323
1324| Name| Type  | Mandatory| Description                     |
1325| ------ | ------ | ---- | :------------------------ |
1326| url    | string | Yes  | URL of the cookie to set. A complete URL is recommended.|
1327| value  | string | Yes  | Cookie value to set.     |
1328
1329**Error codes**
1330
1331For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
1332
1333| ID| Error Message                                              |
1334| -------- | ------------------------------------------------------ |
1335| 17100002 | URL error. No valid cookie found for the specified URL.  |
1336| 17100005 | The provided cookie value is invalid. It must follow the format specified in RFC 6265. |
1337| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
1338
1339**Example**
1340
1341```ts
1342// xxx.ets
1343import { webview } from '@kit.ArkWeb';
1344import { BusinessError } from '@kit.BasicServicesKit';
1345
1346@Entry
1347@Component
1348struct WebComponent {
1349  controller: webview.WebviewController = new webview.WebviewController();
1350
1351  build() {
1352    Column() {
1353      Button('setCookie')
1354        .onClick(() => {
1355          try {
1356            webview.WebCookieManager.setCookie('https://www.example.com', 'a=b');
1357          } catch (error) {
1358            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
1359          }
1360        })
1361      Web({ src: 'www.example.com', controller: this.controller })
1362    }
1363  }
1364}
1365```
1366
1367## deleteEntireCookie<sup>(deprecated)</sup>
1368
1369static deleteEntireCookie(): void
1370
1371Deletes all cookies.
1372
1373> **NOTE**
1374>
1375> This API is supported since API version 9 and deprecated since API version 11. You are advised to use [clearAllCookiesSync](#clearallcookiessync11) instead.
1376
1377**System capability**: SystemCapability.Web.Webview.Core
1378
1379**Example**
1380
1381```ts
1382// xxx.ets
1383import { webview } from '@kit.ArkWeb';
1384
1385@Entry
1386@Component
1387struct WebComponent {
1388  controller: webview.WebviewController = new webview.WebviewController();
1389
1390  build() {
1391    Column() {
1392      Button('deleteEntireCookie')
1393        .onClick(() => {
1394          webview.WebCookieManager.deleteEntireCookie();
1395        })
1396      Web({ src: 'www.example.com', controller: this.controller })
1397    }
1398  }
1399}
1400```
1401
1402## deleteSessionCookie<sup>(deprecated)</sup>
1403
1404static deleteSessionCookie(): void
1405
1406Deletes all session cookies.
1407
1408> **NOTE**
1409>
1410> This API is supported since API version 9 and deprecated since API version 11. You are advised to use [clearSessionCookieSync](#clearsessioncookiesync11) instead.
1411
1412**System capability**: SystemCapability.Web.Webview.Core
1413
1414**Example**
1415
1416```ts
1417// xxx.ets
1418import { webview } from '@kit.ArkWeb';
1419
1420@Entry
1421@Component
1422struct WebComponent {
1423  controller: webview.WebviewController = new webview.WebviewController();
1424
1425  build() {
1426    Column() {
1427      Button('deleteSessionCookie')
1428        .onClick(() => {
1429          webview.WebCookieManager.deleteSessionCookie();
1430        })
1431      Web({ src: 'www.example.com', controller: this.controller })
1432    }
1433  }
1434}
1435```
1436