1# @ohos.app.ability.UIExtensionContentSession (UI Operation Class for ExtensionAbilities with UI) 2 3**UIExtensionContentSession** is an instance created when the [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md) loads UI content. When the UIExtensionComponent starts a UIExtensionAbility, the UIExtensionAbility creates a UIExtensionContentSession instance and returns it through the [onSessionCreate](js-apis-app-ability-uiExtensionAbility.md#uiextensionabilityonsessioncreate) callback. One UIExtensionComponent corresponds to one **UIExtensionContentSession** instance, which provides methods such as UI loading and result notification. The **UIExtensionContentSession** instances of multiple UIExtensionAbilities are operated separately. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> The APIs of this module can be used only in the stage model. 10 11## Modules to Import 12 13```ts 14import { UIExtensionContentSession } from '@kit.AbilityKit'; 15``` 16 17## UIExtensionContentSession.loadContent 18 19loadContent(path: string, storage?: LocalStorage): void 20 21Loads content from a page associated with a local storage to the window corresponding to the current UIExtensionComponent. 22 23**System capability**: SystemCapability.Ability.AbilityRuntime.Core 24 25**Parameters** 26 27| Name | Type | Mandatory| Description | 28| ------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 29| path | string | Yes | Path of the page from which the content will be loaded. | 30| storage | [LocalStorage](../../quick-start/arkts-localstorage.md) | No | A storage unit, which provides storage for variable state properties and non-variable state properties of an application. This parameter is left blank by default.| 31 32**Error codes** 33 34For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 35 36| ID| Error Message| 37| ------- | -------------------------------- | 38| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 39| 16000050 | Internal error. | 40 41**Example** 42 43```ts 44import { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 45 46export default class UIExtAbility extends UIExtensionAbility { 47 // ... 48 49 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 50 let storage: LocalStorage = new LocalStorage(); 51 storage.setOrCreate('session', session); 52 session.loadContent('pages/Extension', storage); 53 } 54 55 // ... 56} 57``` 58 59## UIExtensionContentSession.terminateSelf 60 61terminateSelf(callback: AsyncCallback<void>): void 62 63Stops the window object corresponding to this **UIExtensionContentSession** instance. This API uses an asynchronous callback to return the result. 64 65**System capability**: SystemCapability.Ability.AbilityRuntime.Core 66 67**Parameters** 68 69| Name| Type| Mandatory| Description| 70| -------- | -------- | -------- | -------- | 71| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the window object is stopped, **err** is **undefined**; otherwise, **err** is an error object.| 72 73**Error codes** 74 75For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 76 77| ID| Error Message| 78| ------- | -------------------------------- | 79| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 80 81**Example** 82 83```ts 84import { UIExtensionContentSession } from '@kit.AbilityKit'; 85import { BusinessError } from '@kit.BasicServicesKit'; 86 87let storage = LocalStorage.getShared(); 88 89@Entry(storage) 90@Component 91struct Index { 92 private session: UIExtensionContentSession | undefined = 93 storage.get<UIExtensionContentSession>('session'); 94 95 build() { 96 RelativeContainer() { 97 Button('TerminateSelf') 98 .onClick(() => { 99 this.session?.terminateSelf((err: BusinessError) => { 100 if (err) { 101 console.error(`Failed to terminate self, code: ${err.code}, msg: ${err.message}`); 102 return; 103 } 104 console.info(`Successed in terminating self.`); 105 }); 106 107 storage.clear(); 108 }) 109 } 110 .height('100%') 111 .width('100%') 112 } 113} 114``` 115 116## UIExtensionContentSession.terminateSelf 117 118terminateSelf(): Promise<void> 119 120Stops the window object corresponding to this **UIExtensionContentSession** instance. This API uses a promise to return the result. 121 122**System capability**: SystemCapability.Ability.AbilityRuntime.Core 123 124**Return value** 125 126| Type| Description| 127| -------- | -------- | 128| Promise<void> | Promise that returns no value.| 129 130**Example** 131 132```ts 133import { UIExtensionContentSession } from '@kit.AbilityKit'; 134import { BusinessError } from '@kit.BasicServicesKit'; 135 136let storage = LocalStorage.getShared(); 137 138@Entry(storage) 139@Component 140struct Index { 141 private session: UIExtensionContentSession | undefined = 142 storage.get<UIExtensionContentSession>('session'); 143 144 build() { 145 RelativeContainer() { 146 Button('TerminateSelf') 147 .onClick(() => { 148 this.session?.terminateSelf() 149 .then(() => { 150 console.info(`Successed in terminating self.`); 151 }) 152 .catch((err: BusinessError) => { 153 console.error(`Failed to terminate self, code: ${err.code}, msg: ${err.message}`); 154 }); 155 156 storage.clear(); 157 }) 158 } 159 .height('100%') 160 .width('100%') 161 } 162} 163``` 164 165## UIExtensionContentSession.terminateSelfWithResult 166 167terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void 168 169Stops the window object corresponding to this **UIExtensionContentSession** instance and returns the result to the UIExtensionComponent. This API uses an asynchronous callback to return the result. 170 171**System capability**: SystemCapability.Ability.AbilityRuntime.Core 172 173**Parameters** 174 175| Name| Type| Mandatory| Description| 176| -------- | -------- | -------- | -------- | 177| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Result returned to the UIExtensionComponent.| 178| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the window object is stopped, **err** is **undefined**; otherwise, **err** is an error object.| 179 180**Error codes** 181 182For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 183 184| ID| Error Message| 185| ------- | -------------------------------- | 186| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 187 188**Example** 189 190```ts 191import { UIExtensionContentSession, common } from '@kit.AbilityKit'; 192import { BusinessError } from '@kit.BasicServicesKit'; 193 194let storage = LocalStorage.getShared(); 195 196@Entry(storage) 197@Component 198struct Index { 199 private session: UIExtensionContentSession | undefined = 200 storage.get<UIExtensionContentSession>('session'); 201 202 build() { 203 RelativeContainer() { 204 Button('TerminateSelfWithResult') 205 .onClick(() => { 206 let abilityResult: common.AbilityResult = { 207 resultCode: 0, 208 want: { 209 bundleName: 'com.ohos.uiextensioncontentsession', 210 parameters: { 211 'result': 123456 212 } 213 } 214 }; 215 216 this.session?.terminateSelfWithResult(abilityResult, (err: BusinessError) => { 217 if (err) { 218 console.error(`Failed to terminate self with result, code: ${err.code}, msg: ${err.message}`); 219 return; 220 } 221 console.info(`Successed in terminating self with result.`); 222 }); 223 224 storage.clear(); 225 }) 226 } 227 .height('100%') 228 .width('100%') 229 } 230} 231``` 232 233## UIExtensionContentSession.terminateSelfWithResult 234 235terminateSelfWithResult(parameter: AbilityResult): Promise<void> 236 237Stops the window object corresponding to this **UIExtensionContentSession** instance and returns the result to the UIExtensionComponent. This API uses a promise to return the result. 238 239**System capability**: SystemCapability.Ability.AbilityRuntime.Core 240 241**Parameters** 242 243| Name| Type| Mandatory| Description| 244| -------- | -------- | -------- | -------- | 245| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Result returned to the UIExtensionComponent.| 246 247**Return value** 248 249| Type| Description| 250| -------- | -------- | 251| Promise<void> | Promise that returns no value.| 252 253**Error codes** 254 255For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 256 257| ID| Error Message| 258| ------- | -------------------------------- | 259| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 260 261**Example** 262 263```ts 264import { UIExtensionContentSession, common } from '@kit.AbilityKit'; 265import { BusinessError } from '@kit.BasicServicesKit'; 266 267let storage = LocalStorage.getShared(); 268 269@Entry(storage) 270@Component 271struct Index { 272 private session: UIExtensionContentSession | undefined = 273 storage.get<UIExtensionContentSession>('session'); 274 275 build() { 276 RelativeContainer() { 277 Button('TerminateSelfWithResult') 278 .onClick(() => { 279 let abilityResult: common.AbilityResult = { 280 resultCode: 0, 281 want: { 282 bundleName: 'com.ohos.uiextensioncontentsession', 283 parameters: { 284 'result': 123456 285 } 286 } 287 }; 288 289 this.session?.terminateSelfWithResult(abilityResult) 290 .then(() => { 291 console.info(`Successed in terminating self with result.`); 292 }) 293 .catch((err: BusinessError) => { 294 console.error(`Failed to terminate self with result, code: ${err.code}, msg: ${err.message}`); 295 }); 296 297 storage.clear(); 298 }) 299 } 300 .height('100%') 301 .width('100%') 302 } 303} 304``` 305 306## UIExtensionContentSession.setWindowPrivacyMode 307 308setWindowPrivacyMode(isPrivacyMode: boolean): Promise<void> 309 310Sets whether the window is in privacy mode. A window in privacy mode cannot be captured or recorded. This API uses a promise to return the result. 311 312**System capability**: SystemCapability.Ability.AbilityRuntime.Core 313 314**Required permissions**: ohos.permission.PRIVACY_WINDOW 315 316**Parameters** 317 318| Name| Type| Mandatory| Description| 319| ------------- | ------- | -- | ----------------------------------------------------- | 320| isPrivacyMode | boolean | Yes| Whether the window is in privacy mode. The value **true** means that the window is in privacy mode, and **false** means the opposite.| 321 322**Return value** 323 324| Type| Description| 325| ------------------- | ------------------------ | 326| Promise<void> | Promise that returns no value.| 327 328**Error codes** 329 330For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 331 332| ID| Error Message| 333| ------- | -------------------------------- | 334| 201 | The application does not have permission to call the interface. | 335| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 336 337**Example** 338 339```ts 340import { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 341import { BusinessError } from '@kit.BasicServicesKit'; 342 343export default class UIExtAbility extends UIExtensionAbility { 344 // ... 345 346 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 347 let isPrivacyMode: boolean = true; 348 try { 349 session.setWindowPrivacyMode(isPrivacyMode) 350 .then(() => { 351 console.info(`Successed in setting window to privacy mode.`); 352 }) 353 .catch((err: BusinessError) => { 354 console.error(`Failed to set window to privacy mode, code: ${err.code}, msg: ${err.message}`); 355 }); 356 } catch (e) { 357 let code = (e as BusinessError).code; 358 let msg = (e as BusinessError).message; 359 console.error(`Failed to set window to privacy mode, code: ${code}, msg: ${msg}`); 360 } 361 } 362 363 // ... 364} 365``` 366 367## UIExtensionContentSession.setWindowPrivacyMode 368 369setWindowPrivacyMode(isPrivacyMode: boolean, callback: AsyncCallback<void>): void 370 371Sets whether the window is in privacy mode. A window in privacy mode cannot be captured or recorded. This API uses an asynchronous callback to return the result. 372 373**System capability**: SystemCapability.Ability.AbilityRuntime.Core 374 375**Required permissions**: ohos.permission.PRIVACY_WINDOW 376 377**Parameters** 378 379| Name| Type| Mandatory| Description| 380| ------------- | ------------------------- | -- | ------------------------------------------------------ | 381| isPrivacyMode | boolean | Yes| Whether the window is in privacy mode. The value **true** means that the window is in privacy mode, and **false** means the opposite. | 382| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the setting is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 383 384**Error codes** 385 386For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 387 388| ID| Error Message| 389| ------- | -------------------------------- | 390| 201 | The application does not have permission to call the interface. | 391| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 392 393**Example** 394 395```ts 396import { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 397import { BusinessError } from '@kit.BasicServicesKit'; 398 399export default class UIExtAbility extends UIExtensionAbility { 400 // ... 401 402 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 403 let isPrivacyMode: boolean = true; 404 try { 405 session.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { 406 if (err) { 407 console.error(`Failed to set window to privacy mode, code: ${err.code}, msg: ${err.message}`); 408 return; 409 } 410 console.info(`Successed in setting window to privacy mode.`); 411 }); 412 } catch (e) { 413 let code = (e as BusinessError).code; 414 let msg = (e as BusinessError).message; 415 console.error(`Failed to set window to privacy mode, code: ${code}, msg: ${msg}`); 416 } 417 } 418 419 // ... 420} 421``` 422 423## UIExtensionContentSession.startAbilityByType<sup>11+</sup> 424 425startAbilityByType(type: string, wantParam: Record<string, Object>, 426 abilityStartCallback: AbilityStartCallback, callback: AsyncCallback\<void>): void 427 428Implicitly starts a given type of UIExtensionAbility. This API uses an asynchronous callback to return the result. It can be called only by applications running in the foreground. 429 430**System capability**: SystemCapability.Ability.AbilityRuntime.Core 431 432**Parameters** 433 434| Name| Type| Mandatory| Description| 435| -------- | -------- | -------- | -------- | 436| type | string | Yes| Type of the UIExtensionAbility to start.<!--Del--> For details, see [Starting an Application of the Specified Type](../../application-models/start-intent-panel.md#matching-rules).<!--DelEnd-->| 437| wantParam | Record<string, Object> | Yes| Extended parameter.| 438| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | Yes| Callback used to return the detailed error information if the startup fails.| 439| callback | AsyncCallback\<void> | Yes|Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 440 441**Error codes** 442 443For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 444 445| ID| Error Message| 446| ------- | -------------------------------- | 447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 448| 16000050 | Internal error. | 449 450**Example** 451 452```ts 453import { UIExtensionContentSession, UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 454import { BusinessError } from '@kit.BasicServicesKit'; 455 456export default class UIExtAbility extends UIExtensionAbility { 457 // ... 458 459 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 460 let wantParams: Record<string, Object> = { 461 'sceneType': 1 462 }; 463 let abilityStartCallback: common.AbilityStartCallback = { 464 onError: (code: number, name: string, message: string) => { 465 console.error(`onError, code: ${code}, name: ${name}, msg: ${message}`); 466 }, 467 onResult: (result: common.AbilityResult) => { 468 console.info(`onResult, result: ${JSON.stringify(result)}`); 469 } 470 }; 471 472 session.startAbilityByType('test', wantParams, abilityStartCallback, (err: BusinessError) => { 473 if (err) { 474 console.error(`Failed to startAbilityByType, code: ${err.code}, msg: ${err.message}`); 475 return; 476 } 477 console.info(`Successed in startAbilityByType`); 478 }); 479 } 480 481 // ... 482} 483``` 484 485## UIExtensionContentSession.startAbilityByType<sup>11+</sup> 486 487startAbilityByType(type: string, wantParam: Record<string, Object>, 488 abilityStartCallback: AbilityStartCallback): Promise\<void> 489 490Implicitly starts a given type of UIExtensionAbility. This API uses a promise to return the result. It can be called only by applications running in the foreground. 491 492**System capability**: SystemCapability.Ability.AbilityRuntime.Core 493 494**Parameters** 495 496| Name| Type| Mandatory| Description| 497| -------- | -------- | -------- | -------- | 498| type | string | Yes| Type of the UIExtensionAbility to start.<!--Del--> For details, see [Starting an Application of the Specified Type](../../application-models/start-intent-panel.md#matching-rules).<!--DelEnd-->| 499| wantParam | Record<string, Object> | Yes| Extended parameter.| 500| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | Yes| Callback used to return the detailed error information if the startup fails.| 501 502**Return value** 503 504| Type| Description| 505| -------- | -------- | 506| Promise\<void> | Promise that returns no value.| 507 508**Error codes** 509 510For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 511 512| ID| Error Message| 513| ------- | -------------------------------- | 514| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 515| 16000050 | Internal error. | 516 517**Example** 518 519```ts 520import { UIExtensionContentSession, UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 521import { BusinessError } from '@kit.BasicServicesKit'; 522 523export default class UIExtAbility extends UIExtensionAbility { 524 // ... 525 526 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 527 let wantParams: Record<string, Object> = { 528 'sceneType': 1 529 }; 530 let abilityStartCallback: common.AbilityStartCallback = { 531 onError: (code: number, name: string, message: string) => { 532 console.error(`onError, code: ${code}, name: ${name}, msg: ${message}`); 533 }, 534 onResult: (result: common.AbilityResult) => { 535 console.info(`onResult, result: ${JSON.stringify(result)}`); 536 } 537 }; 538 539 session.startAbilityByType('test', wantParams, abilityStartCallback) 540 .then(() => { 541 console.info(`Successed in startAbilityByType`); 542 }) 543 .catch((err: BusinessError) => { 544 console.error(`Failed to startAbilityByType, code: ${err.code}, msg: ${err.message}`); 545 }); 546 } 547 548 // ... 549} 550``` 551 552## UIExtensionContentSession.getUIExtensionWindowProxy<sup>12+</sup> 553 554getUIExtensionWindowProxy(): uiExtension.WindowProxy 555 556Obtains the window proxy of this UIExtensionAbility. 557 558**System capability**: SystemCapability.Ability.AbilityRuntime.Core 559 560**Return value** 561 562| Type| Description| 563| -------- | -------- | 564| uiExtension.WindowProxy | Window proxy.| 565 566**Error codes** 567 568For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 569 570| ID| Error Message| 571| ------- | -------------------------------- | 572| 16000050 | Internal error. | 573 574**Example** 575 576```ts 577// Index.ets 578import { UIExtensionContentSession } from '@kit.AbilityKit'; 579import uiExtension from '@ohos.arkui.uiExtension'; 580 581let storage = LocalStorage.getShared(); 582 583@Entry(storage) 584@Component 585struct Extension { 586 @State message: string = 'EmbeddedUIExtensionAbility Index'; 587 private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 588 private extensionWindow: uiExtension.WindowProxy | undefined = this.session?.getUIExtensionWindowProxy(); 589 590 aboutToAppear(): void { 591 this.extensionWindow?.on('windowSizeChange', (size) => { 592 console.info(`size = ${JSON.stringify(size)}`); 593 }); 594 this.extensionWindow?.on('avoidAreaChange', (info) => { 595 console.info(`type = ${JSON.stringify(info.type)}, area = ${JSON.stringify(info.area)}`); 596 }); 597 } 598 599 aboutToDisappear(): void { 600 this.extensionWindow?.off('windowSizeChange'); 601 this.extensionWindow?.off('avoidAreaChange'); 602 } 603 604 build() { 605 Column() { 606 Text(this.message) 607 .fontSize(20) 608 .fontWeight(FontWeight.Bold) 609 } 610 .width('100%') 611 } 612} 613``` 614