目录

Processing the Shared Content on the Sharing Details Page

The sharing details page capability is built upon the UIExtensionAbility for UI embedding. Target apps can develop a sharing details page to receive the shared content via the ShareExtensionAbility. Target apps will be displayed in the recommendation area of the sharing panel, enabling the streamlined handling of shared content through the sharing details page. During development, target apps need to implement the ShareExtensionAbility and register the supported shared data types in the module.json5 file.

If an app is registered in this way, users will be redirected to the sharing details page when they tap the app and can choose to return to the sharing panel from the details page. For details, please refer to the sharing details page.

Development Procedure

  1. Import required modules.

    import { Want, ShareExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit';
    import { systemShare } from '@kit.ShareKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Use the ShareExtensionAbility as the base class to build the sharing ability in the target app. After the ability is started, you can obtain the passed want parameter from the onSessionCreate callback of the ability and parse the want parameter using getSharedData to obtain the shared data.

    export default class TestShareAbility extends ShareExtensionAbility {
      onSessionCreate(want: Want, session: UIExtensionContentSession) {
        systemShare.getSharedData(want)
          .then((data: systemShare.SharedData) => {
            data.getRecords().forEach((record: systemShare.SharedRecord) => {
              // Process the shared data.
            });
            session.loadContent('pages/Index');
          })
          .catch((error: BusinessError) => {
            console.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`);
            session.terminateSelf();
          });
      }
    }
  3. (Optional) Configure social apps to provide contact information through Intents Kit. When a user selects a contact in the recommendation area to share content, the ability registered by the social app can obtain the contact information from the received want data and directly share content to the specified user.

    export default class TestShareAbility extends ShareExtensionAbility {
      onSessionCreate(want: Want, session: UIExtensionContentSession) {
        systemShare.getContactInfo(want)
          .then(async (contact: systemShare.ContactInfo) => {
            // Process contact information. The target users can be specified based on contact type (such as individual or group) and contact ID.
            // Obtain the shared data.
            let data = await systemShare.getSharedData(want);
          })
          .catch((error: BusinessError) => {
            console.error(`Failed to getContactInfo. Code: ${error.code}, message: ${error.message}`);
            // The contact does not exist or an exception occurs when obtaining data.
            session.terminateSelf();
          });
      }
    }
  4. 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.

    "extensionAbilities": [
      {
        "name": "TestShareAbility",
        "srcEntry": "./ets/abilities/TestShareAbility.ts",
        "type": "share", // Support the processing of shared data.
        "description": "xxx",
        "exported": true,
        "label": "$string:xx_label",
        "icon": "$media:icon",
        "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
              }
            ]
          }
        ]
      }
    ]