应用内处理分享内容
=========

目标应用可以通过[UIAbility](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability)构建接收分享内容的界面，并将应用显示到分享面板应用推荐区内，以实现将分享内容传递到目标应用内进行处理。开发时需要接入方实现[UIAbility](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability)并于module.json5中注册支持分享内容的能力。

#### 接口说明

getSharedData接口用于从want中获取分享数据；getContactInfo接口用于从want中获取联系人信息（仅当用户选择联系人分享时有返回值）。具体API说明详见[接口文档](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#getshareddata)。

**表1** 目标应用解析分享数据接口功能介绍

| 接口名 | 描述 |
| :-- | :-- |
| [getSharedData](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#getshareddata)(want: [Want](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inner-ability-want)): Promise<[SharedData](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#shareddata)\> | 从want中获取分享数据 |
| [getContactInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#getcontactinfo)(want: [Want](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inner-ability-want)): Promise<[ContactInfo](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#contactinfo)\> | 从want中获取联系人信息 |

#### 开发步骤

1.  导入相关模块。
    
    import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
    import { window } from '@kit.ArkUI';
    import { systemShare } from '@kit.ShareKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
2.  目标应用可实现[UIAbility](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability)。在Ability被启动后，可以在其[onCreate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability#oncreate)或[onNewWant](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability#onnewwant)回调中获取传入的want参数。将want参数通过[getSharedData](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/share-system-share#getshareddata)解析后得到分享数据。
    
    export default class TestUIAbility extends UIAbility {
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        systemShare.getSharedData(want)
          .then((data: systemShare.SharedData) => {
            data.getRecords().forEach((record: systemShare.SharedRecord) => {
              // 处理分享数据
            });
          })
          .catch((error: BusinessError) => {
            console.error(\`Failed to getSharedData. Code: ${error.code}, message: ${error.message}\`);
            this.context.terminateSelf();
          });
      }
      onWindowStageCreate(windowStage: window.WindowStage): void {
        // Main window is created, set main page for this ability
        windowStage.loadContent('pages/Index', (error) => {
          if (error.code) {
            console.error(\`Failed to load the content. Code: ${error.code}, message: ${error.message}\`);
            return;
          }
          console.info('Succeeded in loading the content.');
        });
      }
    }
    
3.  构建完UIAbility，需要在应用配置文件（src/main/module.json5）的[skills](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/module-configuration-file#skills标签)配置中注册。配置actions为ohos.want.action.sendData；uris需穷举所有支持的数据类型。
    
    "abilities": \[
      {
        "name": "TestUIAbility",
        "srcEntry": "./ets/entryability/TestUIAbility.ets",
        "description": "$string:EntryAbility\_desc",
        "icon": "$media:layered\_image",
        "label": "$string:EntryAbility\_label",
        "startWindowIcon": "$media:startIcon",
        "startWindowBackground": "$color:start\_window\_background",
        "exported": true,
        "skills": \[
          {
            "actions": \[
              "ohos.want.action.sendData"
            \],
            // scheme为预留字段，在此处不生效，配置file仅为示例
            // 目标应用在配置支持接收的数据类型时，需穷举支持的UTD，比如：支持全部图片类型，可声明：general.image
            // maxFileSupported 对于归属指定类型的文件，标识一次支持接收的最大数量。默认为0，代表不支持此类文件的分享。
            // 文件类型归属关系参考：@ohos.data.uniformTypeDescriptor (标准化数据定义与描述)
            "uris": \[
              {
                "scheme": "file",
                "utd": "general.text",
                "maxFileSupported": 1
              },
              {
                "scheme": "file",
                "utd": "general.png",
                "maxFileSupported": 1
              },
              {
                "scheme": "file",
                "utd": "general.jpeg",
                "maxFileSupported": 1
              }
            \]
          }
        \]
      }
    \]