1# UIExtensionComponent 2 3**\<UIExtensionComponent>** is used to embed UIs provided by other applications in the local application UI. The embedded content runs in another process, and the local application does not participate in its layout and rendering. 4 5This component is usually used in modular development scenarios where process isolation is required. 6 7> **NOTE** 8> 9> This component is supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version. 10> 11> The APIs provided by this component are system APIs. 12 13## Restrictions 14 15This component does not support preview. 16 17The ability to be started must be a UIExtensionAbility, an extension ability with UI. For details about how to implement a UIExtensionAbility, see [Implementing UIExtensionAbility](../apis/js-apis-app-ability-uiExtensionAbility.md). 18 19The width and height of the component must be set to non-zero valid values. 20 21## Child Components 22 23Not supported 24 25## APIs 26 27UIExtensionComponent(want: Want) 28 29**Parameters** 30 31| Name | Type | Mandatory | Description | 32| ---- | ---------------------------------------- | ---- | --------------- | 33| want | [Want](../apis/js-apis-app-ability-want.md) | Yes | Ability to start.| 34 35## UIExtensionProxy 36 37Implements a **UIExtensionProxy** instance to send data from the component host to the started UIExtensionAbility through the connection established between the two parties. 38 39### send 40 41send(data: { [key: string]: Object }): void 42 43**Parameters** 44 45| Name | Type | Mandatory | Description | 46| ---- | ---------------------------------------- | ---- | --------------- | 47| data | { [key: string]: Object } | Yes | Data to send to the started UIExtensionAbility.| 48 49## Attributes 50 51The [universal attributes](ts-universal-attributes-size.md) are supported. 52 53## Events 54 55The [universal events](ts-universal-events-click.md) are not supported. 56 57The events are passed to the remote UIExtensionAbility for processing after coordinate conversion. 58 59The following events are supported: 60 61### onRemoteReady 62 63onRemoteReady(callback: [Callback](../apis/js-apis-base.md#callback)\<UIExtensionProxy>) 64 65Invoked when the connection to the remote UIExtensionAbility is set up, that is, the UIExtensionAbility is ready to receive data through the proxy. 66 67**Parameters** 68 69| Name | Type | Description | 70| ---------------------------- | ------ | ------------------------------------------------------------ | 71| proxy | UIExtensionProxy | Proxy used to send data to the remote UIExtensionAbility. | 72 73### onReceive 74 75onReceive(callback: [Callback](../apis/js-apis-base.md#callback)\<{ [key: string]: Object }>) 76 77Invoked when the data sent by the started UIExtensionAbility is received. 78 79**Parameters** 80 81| Name | Type | Description | 82| ---------------------------- | ------ | ------------------------------------------------------------ | 83| data | { [key: string]: Object } | Data from the remote UIExtensionAbility. | 84 85### onResult 86 87onResult(callback: [Callback](../apis/js-apis-base.md#callback)\<{code: number; want?: Want}>) 88 89Invoked when the started UIExtensionAbility calls **terminateSelfWithResult**. After this callback is invoked, **OnRelease** is invoked. 90 91The result data of the remote UIExtensionAbility can be processed in this callback. For details, see [AbilityResult](../apis/js-apis-inner-ability-abilityResult.md). 92 93**Parameters** 94 95| Name | Type | Description | 96| ---------------------------- | ------ | ------------------------------------------------------------ | 97| code | number | Result code from the remote UIExtensionAbility. | 98| want | Want | [Want](../apis/js-apis-app-ability-want.md) of the result from the remote UIExtensionAbility.| 99 100### onRelease 101 102onRelease(callback: [Callback](../apis/js-apis-base.md#callback)\<number>) 103 104Invoked when the started UIExtensionAbility is destroyed. 105 106If the UIExtensionAbility is destroyed correctly by calling **terminateSelfWithResult** or **terminateSelf**, the value of **releaseCode** is **0**. 107 108If the UIExtensionAbility is destroyed because it crashes or is forced stopped, the value of **releaseCode** is **1**. 109 110**Parameters** 111 112| Name | Type | Description | 113| ---------------------------- | ------ | ------------------------------------------------------------ | 114| releaseCode | number | Code that indicates how the remote UIExtensionAbility is destroyed. The value **0** means normal destruction, and **1** means abnormal destruction. | 115 116### onError 117 118onError(callback:[ErrorCallback](../apis/js-apis-base.md#errorcallback)) 119 120Invoked when an exception occurs during the running of the UIExtensionAbility. You can obtain the error information based on the **code**, **name**, and **message** parameters in the callback and rectify the exception accordingly. 121 122**Parameters** 123 124| Name | Type | Description | 125| ---------------------------- | ------ | ------------------------------------------------------------ | 126| err | [BusinessError](../apis/js-apis-base.md#businesserror) | Error information. | 127 128## Example 129 130This example shows only the method used by the component and the UIExtensionAbility. For the code to run properly, you need to install the ability whose **bundleName** is **"com.example.uiextensionprovider"** and **abilityName** is **"UIExtensionProvider"** on the device. 131 132```ts 133// Component usage example: 134import Want from '@ohos.app.ability.Want'; 135 136@Entry 137@Component 138struct Index { 139 @State message: string = 'Hello World' 140 private myProxy: UIExtensionProxy | null = null; 141 want: Want = { 142 bundleName: "com.example.uiextensionprovider", 143 abilityName: "UIExtensionProvider", 144 parameters: { "x": 12345, "y": "data" } 145 } 146 147 build() { 148 Row() { 149 Column() { 150 Text(this.message).fontColor(Color.Red) 151 UIExtensionComponent(this.want) 152 .size({ width: "100%", height: "100%" }) 153 .onRemoteReady((proxy: UIExtensionProxy) => { 154 this.message = "remote ready" 155 this.myProxy = proxy 156 }) 157 .onReceive((data: Object) => { 158 this.message = JSON.stringify(data) 159 }) 160 .onRelease((releaseCode: number) => { 161 this.message = "Release: " + releaseCode 162 }) 163 .onResult((data: Object) => { 164 this.message = JSON.stringify(data) 165 }) 166 .onError((error: ErrorObject) => { 167 this.message = "onError: " + error.code + ", name: " + error.name + ", message: " + error.message 168 }) 169 Button("sendData").onClick(() => { 170 if (this.myProxy != null) { 171 let a: Record<string, number> = { "x": 5678910 }; 172 this.myProxy.send(a) 173 } 174 }) 175 } 176 .width("100%") 177 } 178 .height('100%') 179 } 180} 181 182interface ErrorObject { 183 code: number; 184 name: string; 185 message: string; 186} 187``` 188 189```ts 190// Extension entry point file UIExtensionProvider.ts 191import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility' 192import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession' 193import Want from '@ohos.app.ability.Want'; 194const TAG: string = '[UIExtAbility]' 195export default class UIExtAbility extends UIExtensionAbility { 196 onCreate() { 197 console.log(TAG, `UIExtAbility onCreate`) 198 } 199 200 onForeground() { 201 console.log(TAG, `UIExtAbility onForeground`) 202 } 203 204 onBackground() { 205 console.log(TAG, `UIExtAbility onBackground`) 206 } 207 208 onDestroy() { 209 console.log(TAG, `UIExtAbility onDestroy`) 210 } 211 212 onSessionCreate(want: Want, session: UIExtensionContentSession) { 213 console.log(TAG, `UIExtAbility onSessionCreate, want: ${JSON.stringify(want)}`) 214 let param: Record<string, UIExtensionContentSession> = { 215 'session': session 216 } 217 let storage: LocalStorage = new LocalStorage(param); 218 session.loadContent('pages/extension', storage); 219 } 220 221 onSessionDestroy(session: UIExtensionContentSession) { 222 console.log(TAG, `UIExtAbility onSessionDestroy`) 223 } 224} 225``` 226 227```ts 228// Extension Ability entry page file extension.ets 229import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession' 230 231let storage = LocalStorage.GetShared() 232 233@Entry(storage) 234@Component 235struct Index { 236 @State message: string = 'UIExtension' 237 @State message2: string = 'message from comp' 238 private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 239 controller: TextInputController = new TextInputController() 240 241 onPageShow() { 242 if (this.session != undefined) { 243 this.session.setReceiveDataCallback((data: Object) => { 244 this.message2 = "data come from comp" 245 this.message = JSON.stringify(data) 246 }) 247 } 248 } 249 250 build() { 251 Row() { 252 Column() { 253 Text(this.message2) 254 Text(this.message) 255 Button("sendData") 256 .onClick(() => { 257 if (this.session != undefined) { 258 let a: Record<string, string> = {"xxx": "data from extension"}; 259 this.session.sendData(a) 260 } 261 }) 262 Button("terminateSelf") 263 .onClick(() => { 264 if (this.session != undefined) { 265 this.session.terminateSelf(); 266 storage.clear(); 267 } 268 }).margin(5) 269 Button("TerminateSelfWithResult") 270 .onClick(() => { 271 if (this.session != undefined) { 272 this.session.terminateSelfWithResult({ 273 "resultCode": 0, 274 "want": { 275 "bundleName": "myName" 276 } 277 }); 278 storage.clear(); 279 } 280 }).margin(5) 281 } 282 .width('100%') 283 } 284 .height('100%') 285 } 286} 287 288``` 289