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