目录

Processing the Shared Content Inside a Target App

You can display the target app in the recommendation area on the sharing panel and use the UIAbility to build the UI for receiving the shared content in the target app, so as to transfer the shared content to the target app for processing. During development, you need to implement the UIAbility and register the supported shared data types in the module.json5 file.

API Description

The getSharedData API obtains the shared data from want. The getContactInfo API obtains contact information from want (a value is returned only when a user has selected a contact to share content). For details about the APIs, please refer to API Reference.

Table 1 APIs used by the target app for parsing the shared data

API Description
getSharedData(want: Want): Promise<SharedData> Obtains the shared data from want.
getContactInfo(want: Want): Promise<ContactInfo> Obtains contact information from want.

Development Procedure

  1. Import required modules.

    import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
    import { window } from '@kit.ArkUI';
    import { systemShare } from '@kit.ShareKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Implement the UIAbility for the target app. After the ability is started, you can obtain the passed want parameter from the onCreate or onNewWant callback of the ability. Then parse the want parameter using getSharedData to obtain the shared data.

    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) => {
              // Process the shared data.
            });
          })
          .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. Complete the skills configuration in the app configuration file (src/main/module.json5). Set actions to ohos.want.action.sendData and uris to all the supported data types.

    "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"
            ],
            // The scheme field is reserved and does not take effect here. The value file is just an example.
            // When configuring the types of data that can be received by the target app, list all the supported UTDs. For example, if all image types are supported, declare general.image.
            // maxFileSupported indicates the maximum number of files of a specified type that can be received at a time. The default value is 0, indicating that files of this type cannot be shared.
            // For details about the relationship between different file types, please refer to @ohos.data.uniformTypeDescriptor (Uniform Data Definition and Description).
            "uris": [
              {
                "scheme": "file",
                "utd": "general.text",
                "maxFileSupported": 1
              },
              {
                "scheme": "file",
                "utd": "general.png",
                "maxFileSupported": 1
              },
              {
                "scheme": "file",
                "utd": "general.jpeg",
                "maxFileSupported": 1
              }
            ]
          }
        ]
      }
    ]