目录

Using App Linking for Application Redirection

Overview

In App Linking, the system directs users to specific content in the target application based on the passed-in URI (HTTPS link). Unlike Deep Linking, users can directly access the content regardless of whether the target application is installed.

When to Use

Working Principles

Procedure for the Target Application

To use App Linking in the target application, perform the following operations:

  1. Declare a domain name.
  2. Associate the application on the developer website.
  3. Add code to the ability of the application to handle the passed-in link.

[h2]Declaring a Domain Name

Configure the module.json5 file of the application to declare the domain name associated with the application, and enable domain name verification:

By default, the skills field contains a skill object, which is used to identify the application entry. Application redirection links should not be configured in this object. Instead, separate skill objects should be used. If there are multiple redirection scenarios, create different skill objects under skills. Otherwise, the configuration does not take effect.

For example, the configuration below declares that the application is associated with the domain name www.example.com.

{
  "module": {
    // ···
    "abilities": [
      {
        // ···
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "ohos.want.action.home"
            ]
          },
          {
            "entities": [
              // entities must contain "entity.system.browsable".
              "entity.system.browsable"
            ],
            "actions": [
              // actions must contain "ohos.want.action.viewData".
              "ohos.want.action.viewData"
            ],
            "uris": [
              {
                // scheme must be set to https.
                "scheme": "https",
                // host must be set to the associated domain name.
                "host": "www.example.com",
                // path is optional. To distinguish between applications that are associated with the same domain name, you are advised to configure this field.
                "path": "path1"
              }
            ],
            // domainVerify must be set to true.
            "domainVerify": true
          } // Add a skill object for redirection. If there are multiple redirection scenarios, create multiple skill objects.
        ]
      },
    // ···
    ],
    // ···
  }
}

[h2]Associating the Application on the Developer Website

Perform the following operations on the developer website to associate the application:

  1. Create the domain name configuration file applinking.json.

    The content is as follows:

    {
     "applinking": {
       "apps": [
         {
           "appIdentifier": "1234"
         }
       ]
     }
    }

    app-identifier is the unique identifier allocated to an application during application signing. It is also the value of the app-identifier field declared in the [HarmonyAppProvision configuration file].

  2. Place the domain name configuration file in a fixed directory on the DNS.

    The fixed directory is as follows:

    https://your.domain.name/.well-known/applinking.json

    For example, if the domain name is www.example.com, place the applinking.json file in the following directory:

    https://www.example.com/.well-known/applinking.json

Implementing Application Redirection (Required for the Caller Application)

The caller application passes in the link of the target application through the UIAbilityContext.openLink API to start the target application.

The openLink API provides two methods for starting the target application.

This section describes method 1, in order to check whether the App Linking configuration is correct. The following is an example.

import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const DOMAIN_NUMBER = 0xF811;
const TAG = '[Sample_AppLinking]';

@Entry
@Component
struct Index {
  build() {
    Button('start link', { type: ButtonType.Capsule, stateEffect: true })
      .width('87%')
      .height('5%')
      .margin({ bottom: '12vp' })
      .onClick(() => {
        let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
        let link: string = 'https://www.example.com/programs?action=showall'; // Use the actual application link.
        // Open the application only in App Linking mode.
        context.openLink(link, { appLinkingOnly: true })
          .then(() => {
            hilog.info(DOMAIN_NUMBER, TAG, 'openlink success.');
          })
          .catch((error: BusinessError) => {
            hilog.error(DOMAIN_NUMBER, TAG, `openlink failed. error:${JSON.stringify(error)}`);
          });
      })
  }
}

If the target application is started, the App Linking configuration of the target application is correct.

FAQs

  1. What should I do when the value of skills in the Modules.json5 file of the application is incorrect?

    Ensure that the value of host is the domain name of the application.

  2. What should I do when the developer website server is incorrectly configured?

    • Check the JSON configuration of the server and ensure that the value of appIdentifier is correct.
    • Check whether the applinking.json file is stored in the correct directory (.well-known). Use a browser to access the JSON file address https://your.domain.name/.well-known/applinking.json and ensure that the file is accessible.
  3. What should I do when the system has not verified the domain name?

    After installing the application on the device, wait for at least 20 seconds to ensure that asynchronous verification is complete.

  4. What is the mapping between applications and domain names?

    They are in a many-to-many relationship. An application can be associated with multiple domain names, and a domain name can be associated with multiple applications.

  5. If a domain name is associated with multiple applications, which application will be started by domain name?

    You can configure the applinking.json file to associate a domain name with multiple applications. If the uris field in the module.json5 file of each application is set to the same value, the system displays a dialog box for users to select the application to start.

    You can also use the path field to distinguish the applications to start. For example, use https://www.example.com/path1 to start target application 1 and use https://www.example.com/path2 to start target application 2.