1# UIServiceProxy
2
3UIServiceProxy functions as a proxy to send data from the UIServiceExtensionAbility client to the server.
4
5
6> **NOTE**
7>
8>  - The initial APIs of this module are supported since API version 13. Newly added APIs will be marked with a superscript to indicate their earliest API version.
9>  - The APIs of this module can be used only in the stage model.
10>  - The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool.
11
12## Modules to Import
13
14```ts
15import { common } from '@kit.AbilityKit';
16```
17
18## UIServiceProxy.sendData
19
20sendData(data: Record\<string, Object>): void
21
22Sends data to the UIServiceExtensionAbility server.
23
24> **NOTE**
25>
26> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md).
27>
28
29
30**System capability**: SystemCapability.Ability.AbilityRuntime.Core
31
32**Parameters**
33
34| Name| Type                  | Read Only| Optional| Description          |
35| ------ | ---------------------- | ---- | ------------ | ------------ |
36| data   | Record\<string, Object> | Yes| No | Data to be sent to the UIServiceExtensionAbility server.|
37
38**Error codes**
39
40For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
41
42| ID| Error Message                    |
43| -------- | ----------------------------|
44| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
45| 16000050 | Internal error.             |
46
47**Example**
48
49```ts
50import { common } from '@kit.AbilityKit';
51import { BusinessError } from '@kit.BasicServicesKit';
52
53const TAG: string = '[Extension] ';
54
55@Entry
56@Component
57struct UIServiceExtensionAbility {
58  comProxy: common.UIServiceProxy | null = null;
59  dataCallBack: common.UIServiceExtensionConnectCallback = {
60    onData: (data: Record<string, Object>) => {
61      console.log(TAG + `dataCallBack received data: `, JSON.stringify(data));
62    },
63    onDisconnect: () => {
64      console.log(TAG + `dataCallBack onDisconnect`);
65      this.comProxy = null;
66    }
67  }
68
69  build() {
70    Scroll() {
71      Column() {
72        // Create a button for connecting to the UIServiceExtensionAbility.
73        Button('connectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true })
74          .margin({
75            top: 5,
76            left: 10,
77            right: 10,
78            bottom: 5
79          })
80          .alignRules({
81            center: { anchor: '__container__', align: VerticalAlign.Center },
82            middle: { anchor: '__container__', align: HorizontalAlign.Center }
83          })
84          .onClick(() => {
85            this.myConnectUIServiceExtensionAbility()
86          });
87      }
88      .width('100%')
89    }
90    .height('100%')
91  }
92
93  // Customize a function for connecting to the UIServiceExtensionAbility.
94  myConnectUIServiceExtensionAbility() {
95    let context = getContext(this) as common.UIAbilityContext;
96    let startWant: Want = {
97      deviceId: '',
98      bundleName: 'com.acts.myapplication',
99      abilityName: 'UiServiceExtensionAbility'
100    };
101
102    try {
103      // Connect to the UIServiceExtensionAbility.
104      context.connectUIServiceExtensionAbility(startWant, this.dataCallBack)
105        .then((proxy: common.UIServiceProxy) => {
106          console.log(TAG + `try to connectUIServiceExtensionAbility ${proxy}}`);
107          this.comProxy = proxy;
108          let formData: Record<string,string> = {
109            'PATH': '/tmp/aaa.jpg'
110          };
111          try {
112            console.log(TAG + `sendData`);
113            // Send data to the UIServiceExtensionAbility.
114            this.comProxy.sendData(formData);
115          } catch (err) {
116            let code = (err as BusinessError).code;
117            let message = (err as BusinessError).message;
118            console.log(TAG + `sendData failed, code is ${code}, message is ${message}`);
119          }
120        }).catch((err: Error) => {
121        let code = (err as BusinessError).code;
122        let message = (err as BusinessError).message;
123        console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`);
124      });
125    } catch (err) {
126      let code = (err as BusinessError).code;
127      let message = (err as BusinessError).message;
128      console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`);
129    }
130  }
131}
132```
133