1e41f4b71Sopenharmony_ci# Using Incognito Mode 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciWhen creating a **\<Web>** component, you can enable incognito mode for it by setting the optional parameter [incognitoMode](../reference/apis-arkweb/ts-basic-components-web.md#incognitomode) to **true**. When incognito mode, data such as cookies and cache data during web page browsing is not stored in local persistent files. This means that such data is lost when the **\<Web>** component is destroyed. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci- Create a [\<Web>](../reference/apis-arkweb/ts-basic-components-web.md#web) component in incognito mode. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci ```ts 9e41f4b71Sopenharmony_ci // xxx.ets 10e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci @Entry 13e41f4b71Sopenharmony_ci @Component 14e41f4b71Sopenharmony_ci struct WebComponent { 15e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci build() { 18e41f4b71Sopenharmony_ci Column() { 19e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 20e41f4b71Sopenharmony_ci } 21e41f4b71Sopenharmony_ci } 22e41f4b71Sopenharmony_ci } 23e41f4b71Sopenharmony_ci ``` 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci- Use [isIncogntoMode](../reference/apis-arkweb/js-apis-webview.md#isincognitomode11) to check whether the current **\<Web>** component is in incognito mode. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci ```ts 28e41f4b71Sopenharmony_ci // xxx.ets 29e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 30e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci @Entry 33e41f4b71Sopenharmony_ci @Component 34e41f4b71Sopenharmony_ci struct WebComponent { 35e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci build() { 38e41f4b71Sopenharmony_ci Column() { 39e41f4b71Sopenharmony_ci Button('isIncognitoMode') 40e41f4b71Sopenharmony_ci .onClick(() => { 41e41f4b71Sopenharmony_ci try { 42e41f4b71Sopenharmony_ci let result = this.controller.isIncognitoMode(); 43e41f4b71Sopenharmony_ci console.log('isIncognitoMode' + result); 44e41f4b71Sopenharmony_ci } catch (error) { 45e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 46e41f4b71Sopenharmony_ci } 47e41f4b71Sopenharmony_ci }) 48e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller }) 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci ``` 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ciIn incognito mode, you can use the following APIs for geolocation information, cookies, and cache data: 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci- Use [allowGeolocation](../reference/apis-arkweb/js-apis-webview.md#allowgeolocation) to allow the specified origin to use the geolocation information. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci ```ts 59e41f4b71Sopenharmony_ci // xxx.ets 60e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 61e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci @Entry 64e41f4b71Sopenharmony_ci @Component 65e41f4b71Sopenharmony_ci struct WebComponent { 66e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 67e41f4b71Sopenharmony_ci origin: string = "file:///"; 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci build() { 70e41f4b71Sopenharmony_ci Column() { 71e41f4b71Sopenharmony_ci Button('allowGeolocation') 72e41f4b71Sopenharmony_ci .onClick(() => { 73e41f4b71Sopenharmony_ci try { 74e41f4b71Sopenharmony_ci // The second parameter of allowGeolocation specifies whether to allow the specified origin to use the geolocation information in incognito mode (true) or in non-incognito mode (false). 75e41f4b71Sopenharmony_ci webview.GeolocationPermissions.allowGeolocation(this.origin, true); 76e41f4b71Sopenharmony_ci } catch (error) { 77e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci }) 80e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci ``` 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci- Use [deleteGeolocation](../reference/apis-arkweb/js-apis-webview.md#deletegeolocation) to clear the geolocation permission status of a specified origin. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci ```ts 89e41f4b71Sopenharmony_ci // xxx.ets 90e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 91e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci @Entry 94e41f4b71Sopenharmony_ci @Component 95e41f4b71Sopenharmony_ci struct WebComponent { 96e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 97e41f4b71Sopenharmony_ci origin: string = "file:///"; 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci build() { 100e41f4b71Sopenharmony_ci Column() { 101e41f4b71Sopenharmony_ci Button('deleteGeolocation') 102e41f4b71Sopenharmony_ci .onClick(() => { 103e41f4b71Sopenharmony_ci try { 104e41f4b71Sopenharmony_ci // The second parameter of deleteGeolocation specifies whether to clear the geolocation permission status of a specified origin in incognito mode (true) or in non-incognito mode (false). 105e41f4b71Sopenharmony_ci webview.GeolocationPermissions.deleteGeolocation(this.origin, true); 106e41f4b71Sopenharmony_ci } catch (error) { 107e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci }) 110e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci ``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci- Use [getAccessibleGeolocation](../reference/apis-arkweb/js-apis-webview.md#getaccessiblegeolocation) to asynchronously obtain the geolocation permission status of the specified origin. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci ```ts 119e41f4b71Sopenharmony_ci // xxx.ets 120e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 121e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci @Entry 124e41f4b71Sopenharmony_ci @Component 125e41f4b71Sopenharmony_ci struct WebComponent { 126e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 127e41f4b71Sopenharmony_ci origin: string = "file:///"; 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci build() { 130e41f4b71Sopenharmony_ci Column() { 131e41f4b71Sopenharmony_ci Button('getAccessibleGeolocation') 132e41f4b71Sopenharmony_ci .onClick(() => { 133e41f4b71Sopenharmony_ci try { 134e41f4b71Sopenharmony_ci // The third parameter of getAccessibleGeolocation specifies whether to obtain the geolocation permission status of the specified origin in incognito mode (true) or in non-incognito mode (false). This API uses an asynchronous callback to return the result. 135e41f4b71Sopenharmony_ci webview.GeolocationPermissions.getAccessibleGeolocation(this.origin, (error, result) => { 136e41f4b71Sopenharmony_ci if (error) { 137e41f4b71Sopenharmony_ci console.log('getAccessibleGeolocationAsync error: ' + JSON.stringify(error)); 138e41f4b71Sopenharmony_ci return; 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci console.log('getAccessibleGeolocationAsync result: ' + result); 141e41f4b71Sopenharmony_ci }, true); 142e41f4b71Sopenharmony_ci } catch (error) { 143e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci }) 146e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci } 150e41f4b71Sopenharmony_ci ``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci- Use [deleteAllData](../reference/apis-arkweb/js-apis-webview.md#deletealldata) to delete all data in the Web SQL Database. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci ```ts 155e41f4b71Sopenharmony_ci // xxx.ets 156e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 157e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci @Entry 160e41f4b71Sopenharmony_ci @Component 161e41f4b71Sopenharmony_ci struct WebComponent { 162e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci build() { 165e41f4b71Sopenharmony_ci Column() { 166e41f4b71Sopenharmony_ci Button('deleteAllData') 167e41f4b71Sopenharmony_ci .onClick(() => { 168e41f4b71Sopenharmony_ci try { 169e41f4b71Sopenharmony_ci // The parameter of deleteAllData specifies whether to delete all data in the Web SQL Database in incognito mode (true) or in non-incognito mode (false). 170e41f4b71Sopenharmony_ci webview.WebStorage.deleteAllData(true); 171e41f4b71Sopenharmony_ci } catch (error) { 172e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci }) 175e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.controller, incognitoMode: true }) 176e41f4b71Sopenharmony_ci .databaseAccess(true) 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci ``` 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci- Use [fetchCookieSync](../reference/apis-arkweb/js-apis-webview.md#fetchcookiesync11) to obtain the cookie corresponding to the specified URL. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci ```ts 185e41f4b71Sopenharmony_ci // xxx.ets 186e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 187e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci @Entry 190e41f4b71Sopenharmony_ci @Component 191e41f4b71Sopenharmony_ci struct WebComponent { 192e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci build() { 195e41f4b71Sopenharmony_ci Column() { 196e41f4b71Sopenharmony_ci Button('fetchCookieSync') 197e41f4b71Sopenharmony_ci .onClick(() => { 198e41f4b71Sopenharmony_ci try { 199e41f4b71Sopenharmony_ci // The second parameter of fetchCookieSync specifies whether to obtain the cookie in incognito mode (true) or in non-incognito mode (false). 200e41f4b71Sopenharmony_ci let value = webview.WebCookieManager.fetchCookieSync('https://www.example.com', true); 201e41f4b71Sopenharmony_ci console.log("fetchCookieSync cookie = " + value); 202e41f4b71Sopenharmony_ci } catch (error) { 203e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci }) 206e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci } 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci ``` 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci- Use [configCookieSync](../reference/apis-arkweb/js-apis-webview.md#configcookiesync11) to set a cookie for the specified URL. 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci ```ts 215e41f4b71Sopenharmony_ci // xxx.ets 216e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 217e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci @Entry 220e41f4b71Sopenharmony_ci @Component 221e41f4b71Sopenharmony_ci struct WebComponent { 222e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci build() { 225e41f4b71Sopenharmony_ci Column() { 226e41f4b71Sopenharmony_ci Button('configCookieSync') 227e41f4b71Sopenharmony_ci .onClick(() => { 228e41f4b71Sopenharmony_ci try { 229e41f4b71Sopenharmony_ci // The third parameter of configCookieSync specifies whether to obtain the cookie for the specified URL in incognito mode (true) or in non-incognito mode (false). 230e41f4b71Sopenharmony_ci webview.WebCookieManager.configCookieSync('https://www.example.com', 'a=b', true); 231e41f4b71Sopenharmony_ci } catch (error) { 232e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 233e41f4b71Sopenharmony_ci } 234e41f4b71Sopenharmony_ci }) 235e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 236e41f4b71Sopenharmony_ci } 237e41f4b71Sopenharmony_ci } 238e41f4b71Sopenharmony_ci } 239e41f4b71Sopenharmony_ci ``` 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci- Use [existCookie](../reference/apis-arkweb/js-apis-webview.md#existcookie) to check whether cookies exist. 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci ```ts 244e41f4b71Sopenharmony_ci // xxx.ets 245e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci @Entry 248e41f4b71Sopenharmony_ci @Component 249e41f4b71Sopenharmony_ci struct WebComponent { 250e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci build() { 253e41f4b71Sopenharmony_ci Column() { 254e41f4b71Sopenharmony_ci Button('existCookie') 255e41f4b71Sopenharmony_ci .onClick(() => { 256e41f4b71Sopenharmony_ci // The parameter of existCookie specifies whether to check for cookies in incognito mode (true) or in non-incognito mode (false). 257e41f4b71Sopenharmony_ci let result = webview.WebCookieManager.existCookie(true); 258e41f4b71Sopenharmony_ci console.log("result: " + result); 259e41f4b71Sopenharmony_ci }) 260e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 261e41f4b71Sopenharmony_ci } 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci ``` 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci- Use [clearAllCookiesSync](../reference/apis-arkweb/js-apis-webview.md#clearallcookiessync11) to delete all cookies. 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci ```ts 269e41f4b71Sopenharmony_ci // xxx.ets 270e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci @Entry 273e41f4b71Sopenharmony_ci @Component 274e41f4b71Sopenharmony_ci struct WebComponent { 275e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci build() { 278e41f4b71Sopenharmony_ci Column() { 279e41f4b71Sopenharmony_ci Button('clearAllCookiesSync') 280e41f4b71Sopenharmony_ci .onClick(() => { 281e41f4b71Sopenharmony_ci // The parameter of clearAllCookiesSync specifies whether to delete all cookies in incognito mode (true) or in non-incognito mode (false). 282e41f4b71Sopenharmony_ci webview.WebCookieManager.clearAllCookiesSync(true); 283e41f4b71Sopenharmony_ci }) 284e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci } 287e41f4b71Sopenharmony_ci } 288e41f4b71Sopenharmony_ci ``` 289