目录

Sharing Links

[h2]Development Procedure

  1. Enable the App Linking service, complete relevant configuration, and test a link of App Linking. For details about how to perform the test, please refer to Testing a Link of App Linking.

  2. Add the association configuration to the skills section in the app configuration file (src/main/module.json5). For details, please refer to Configuring the Associated Domain Name in the module.json5 File.

  3. Use App Linking to initiate a system sharing request.

    import { systemShare } from '@kit.ShareKit';
    import { uniformTypeDescriptor as utd } from '@kit.ArkData';
    import { common } from '@kit.AbilityKit';
    import { image } from '@kit.ImageKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Component
    export default struct Index {
      private async share() {
        // Generate an app icon thumbnail.
        let uiContext: UIContext = this.getUIContext();
        let contextFaker: Context = uiContext.getHostContext() as Context;
        let thumbnailPath = contextFaker.filesDir + '/exampleImage.jpg'; // This is only an example. Replace it with the correct file path.
        let imageSource: image.ImageSource = image.createImageSource(thumbnailPath);
        let imagePacker: image.ImagePacker = image.createImagePacker();
        let buffer: ArrayBuffer = await imagePacker.packToData(imageSource, {
          // The value of format can only be image/jpeg, image/webp, or image/png.
          format: 'image/jpeg',
          // Quality of the output image in JPEG encoding. The value ranges from 0 to 100.
          // You are advised to compress the image to a reasonable size. An oversized image will fail to be shared.
          quality: 30
        });
        // Construct ShareData. You need to configure a valid data record.
        let shareData: systemShare.SharedData = new systemShare.SharedData({
          utd: utd.UniformDataType.HYPERLINK,
          // The link of App Linking is only an example.
          content: 'https://sharekitdemo.drcn.agconnect.link/ZB3p',
          title: 'App name', // If this parameter is not passed, the link will be displayed.
          description: 'App description', // If this parameter is not passed, the description will not be displayed.
          thumbnail: new Uint8Array(buffer) // You are advised to pass the app icon. If no app icon is passed, the default HTML icon will be displayed.
        });
        // Display the sharing panel.
        let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
        let context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
        controller.show(context, {
          previewMode: systemShare.SharePreviewMode.DEFAULT,
          selectionMode: systemShare.SelectionMode.SINGLE
        }).then(() => {
          console.info('ShareController show success.');
        }).catch((error: BusinessError) => {
          console.error(`ShareController show error. code: ${error.code}, message: ${error.message}`);
        });
      }
    
      build() {
        Button('Share')
          .onClick(() => this.share())
      }
    }
  4. The target app processes the link of App Linking. For details, please refer to Processing the Passed Link.

    import { common, OpenLinkOptions } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct Index {
      build() {
        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
          .width('87%')
          .height('5%')
          .margin({ bottom: '12vp' })
          .onClick(() => {
            let uiContext: UIContext = this.getUIContext();
            let context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
            let link: string = "https://www.example.com/programs?action=showall";
            let openLinkOptions: OpenLinkOptions = {
              appLinkingOnly: false
            };
            context.openLink(link, openLinkOptions)
              .then(() => {
                console.info('openlink success.');
              })
              .catch((error: BusinessError) => {
                console.error(`openlink failed. code: ${error.code}, message: ${error.message}`);
              });
          })
      }
    }

    For details about the complete sample code, please refer to Sample Code - Sharing a Link of App Linking to Go to a Specified In-app Page.

[h2]Development Procedure

  1. Import required modules.

    import { systemShare } from '@kit.ShareKit';
    import { uniformTypeDescriptor as utd } from '@kit.ArkData';
    import { common } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Construct the data to be shared.

    // Construct ShareData. You need to configure a valid data record.
    let shareData: systemShare.SharedData = new systemShare.SharedData({
      utd: utd.UniformDataType.HYPERLINK,
      content: 'https://www.vmall.com/index.html?cid=128688', // This is only an example. Replace it with your link to be shared.
      title: 'VMALL',
      description: 'Huawei phone',
      // thumbnail: new Uint8Array() // You are advised to pass a suitable thumbnail. If no thumbnail is passed, the default HTML icon will be displayed.
    });
  3. Add a record.

    shareData.addRecord({
      utd: utd.UniformDataType.HYPERLINK,
      content: 'https://www.vmall.com/index.html?cid=128688', // This is only an example. Replace it with your link to be shared.
      title: 'Test URL',
      description: 'Test description',
    });
  4. Start the sharing panel.

    // Display the sharing panel.
    let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
    let uiContext: UIContext = this.getUIContext();
    let context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
    controller.show(context, {
      selectionMode: systemShare.SelectionMode.SINGLE,
      previewMode: systemShare.SharePreviewMode.DEFAULT,
    }).then(() => {
      console.info('ShareController show success.');
    }).catch((error: BusinessError) => {
      console.error(`ShareController show error. code: ${error.code}, message: ${error.message}`);
    });

    For details about the complete sample code, please refer to Sample Code - Sharing a Common Link to Be Opened Using a Browser.