1e41f4b71Sopenharmony_ci# Using App Linking for Application Redirection
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciIn App Linking, the system directs users to specific content in the target application based on the passed-in URI (HTTPS link). Unlike [Deep Linking](deep-linking-startup.md), users can directly access the content regardless of whether the target application is installed.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci## When to Use
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci* App Linking applies to scenarios with high security requirements. It helps prevent spoofing of the target application.
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci* App Linking applies to scenarios with high requirements on user experience. Users can directly access the content regardless of whether the target application is installed.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Working Principles
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci* App Linking adopts domain name verification, which is unavailable in Deep Linking. Domain name verification helps identify valid applications, making links more secure and reliable.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci* App Linking requires that an HTTPS website be displayed in two modes: application and web page. When the application is installed, the application is preferentially opened to present the content. When the application is not installed, the web page is opened to present the content.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## Procedure for the Target Application
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciTo use App Linking in the target application, perform the following operations:
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci1. Declare a domain name.
26e41f4b71Sopenharmony_ci2. Associate the application on the developer website.
27e41f4b71Sopenharmony_ci3. Add code to the ability of the application to handle the passed-in link.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci### Declaring a Domain Name
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciConfigure the [module.json5 file](../quick-start/module-configuration-file.md) of the application to declare the domain name associated with the application, and enable domain name verification:
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci* The **actions** field must contain **ohos.want.action.viewData**.
35e41f4b71Sopenharmony_ci* The **entities** field must contain **entity.system.browsable**.
36e41f4b71Sopenharmony_ci* The **uris** field must contain an element whose **scheme** is **https** and **host** is a domain name address.
37e41f4b71Sopenharmony_ci* **domainVerify** must be set to **true**.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciFor example, the configuration below declares that the application is associated with the domain name www.example.com.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci```json
42e41f4b71Sopenharmony_ci{
43e41f4b71Sopenharmony_ci  "module": {
44e41f4b71Sopenharmony_ci    // ...
45e41f4b71Sopenharmony_ci    "abilities": [
46e41f4b71Sopenharmony_ci      {
47e41f4b71Sopenharmony_ci        // ...
48e41f4b71Sopenharmony_ci        "skills": [
49e41f4b71Sopenharmony_ci          {
50e41f4b71Sopenharmony_ci            "entities": [
51e41f4b71Sopenharmony_ci              // entities must contain "entity.system.browsable".
52e41f4b71Sopenharmony_ci              "entity.system.browsable"
53e41f4b71Sopenharmony_ci            ],
54e41f4b71Sopenharmony_ci            "actions": [
55e41f4b71Sopenharmony_ci              // actions must contain "ohos.want.action.viewData".
56e41f4b71Sopenharmony_ci              "ohos.want.action.viewData"
57e41f4b71Sopenharmony_ci            ],
58e41f4b71Sopenharmony_ci            "uris": [
59e41f4b71Sopenharmony_ci              {
60e41f4b71Sopenharmony_ci                // scheme must be set to https.
61e41f4b71Sopenharmony_ci                "scheme": "https",
62e41f4b71Sopenharmony_ci                // host must be set to the associated domain name.
63e41f4b71Sopenharmony_ci                "host": "www.example.com",
64e41f4b71Sopenharmony_ci                // path is optional. To distinguish between applications that are associated with the same domain name, you are advised to configure this field.
65e41f4b71Sopenharmony_ci                "path": "path1"
66e41f4b71Sopenharmony_ci              }
67e41f4b71Sopenharmony_ci            ],
68e41f4b71Sopenharmony_ci            // domainVerify must be set to true.
69e41f4b71Sopenharmony_ci           "domainVerify": true
70e41f4b71Sopenharmony_ci          }
71e41f4b71Sopenharmony_ci        ]
72e41f4b71Sopenharmony_ci      }
73e41f4b71Sopenharmony_ci    ]
74e41f4b71Sopenharmony_ci  }
75e41f4b71Sopenharmony_ci}
76e41f4b71Sopenharmony_ci```
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci### Associating the Application on the Developer Website
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ciPerform the following operations on the developer website to associate the application:
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci1. Create the domain name configuration file **applinking.json**.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci   The content is as follows:
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci   ```json
87e41f4b71Sopenharmony_ci   {
88e41f4b71Sopenharmony_ci    "applinking": {
89e41f4b71Sopenharmony_ci      "apps": [
90e41f4b71Sopenharmony_ci        {
91e41f4b71Sopenharmony_ci          "appIdentifier": "1234"
92e41f4b71Sopenharmony_ci        }
93e41f4b71Sopenharmony_ci      ]
94e41f4b71Sopenharmony_ci    }
95e41f4b71Sopenharmony_ci   }
96e41f4b71Sopenharmony_ci   ```
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci   **app-identifer** is the unique identifier allocated to an application during application signing. It is also the value of the **app-identifer** field declared in the [HarmonyAppProvision configuration file](../security/app-provision-structure.md).
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci1. Place the domain name configuration file in a fixed directory on the DNS.
101e41f4b71Sopenharmony_ci   
102e41f4b71Sopenharmony_ci   The fixed directory is as follows:
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci   > https://*your.domain.name*/.well-known/applinking.json
105e41f4b71Sopenharmony_ci   
106e41f4b71Sopenharmony_ci   For example, if the domain name is www.example.com, place the **applinking.json** file in the following directory:
107e41f4b71Sopenharmony_ci   `https://www.example.com/.well-known/applinking.json`
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci### Adding Code to the Ability of the Application to Handle the Passed-in Link
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ciAdd code to the **onCreate()** or **onNewWant()** lifecycle callback of the ability (such as EntryAbility) of the application to handle the passed-in link.
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci```ts
115e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
116e41f4b71Sopenharmony_ciimport { url } from '@kit.ArkTS';
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
119e41f4b71Sopenharmony_ci  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
120e41f4b71Sopenharmony_ci    // Obtain the input link information from want.
121e41f4b71Sopenharmony_ci    // For example, the input URL is https://www.example.com/programs?action=showall.
122e41f4b71Sopenharmony_ci    let uri = want?.uri 
123e41f4b71Sopenharmony_ci    if (uri) {
124e41f4b71Sopenharmony_ci      // Parse the query parameter from the link. You can perform subsequent processing based on service requirements.
125e41f4b71Sopenharmony_ci      let urlObject = url.URL.parseURL(want?.uri);
126e41f4b71Sopenharmony_ci      let action = urlObject.params.get('action')
127e41f4b71Sopenharmony_ci      // For example, if action is set to showall, all programs are displayed.
128e41f4b71Sopenharmony_ci      if (action === "showall") {
129e41f4b71Sopenharmony_ci         // ...
130e41f4b71Sopenharmony_ci      }
131e41f4b71Sopenharmony_ci    }
132e41f4b71Sopenharmony_ci  }
133e41f4b71Sopenharmony_ci}
134e41f4b71Sopenharmony_ci```
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci## Implementing Application Redirection (Required for the Caller Application)
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ciThe caller application passes in the link of the target application through the **UIAbilityContext.openLink** API to start the target application.
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ciThe **openLink** API provides two methods for starting the target application.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci  - Method 1: Open the application only in App Linking mode.
143e41f4b71Sopenharmony_ci    
144e41f4b71Sopenharmony_ci    In this mode, **appLinkingOnly** is set to **true**. If a matching application is found, that application is directly opened. If no application matches, an exception is thrown.
145e41f4b71Sopenharmony_ci    
146e41f4b71Sopenharmony_ci  - Method 2: Open the application preferentially in App Linking mode.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci    In this mode, **appLinkingOnly** is set to **false** or uses the default value. App Linking is preferentially used to start the target application. If a matching application is found, that application is directly opened. If no application matches, the system attempts to open the application in Deep Linking mode.
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ciThis section describes method 1, in order to check whether the App Linking configuration is correct. The following is an example.
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci```ts
153e41f4b71Sopenharmony_ciimport common from '@ohos.app.ability.common';
154e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base';
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci@Entry
157e41f4b71Sopenharmony_ci@Component
158e41f4b71Sopenharmony_cistruct Index {
159e41f4b71Sopenharmony_ci  build() {
160e41f4b71Sopenharmony_ci    Button('start link', { type: ButtonType.Capsule, stateEffect: true })
161e41f4b71Sopenharmony_ci      .width('87%')
162e41f4b71Sopenharmony_ci      .height('5%')
163e41f4b71Sopenharmony_ci      .margin({ bottom: '12vp' })
164e41f4b71Sopenharmony_ci      .onClick(() => {
165e41f4b71Sopenharmony_ci        let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
166e41f4b71Sopenharmony_ci        let link: string = "https://www.example.com/programs?action=showall";
167e41f4b71Sopenharmony_ci        // Open the application only in App Linking mode.
168e41f4b71Sopenharmony_ci        context.openLink(link, { appLinkingOnly: true })
169e41f4b71Sopenharmony_ci          .then(() => {
170e41f4b71Sopenharmony_ci            console.info('openlink success.');
171e41f4b71Sopenharmony_ci          })
172e41f4b71Sopenharmony_ci          .catch((error: BusinessError) => {
173e41f4b71Sopenharmony_ci            console.error(`openlink failed. error:${JSON.stringify(error)}`);
174e41f4b71Sopenharmony_ci          });
175e41f4b71Sopenharmony_ci      })
176e41f4b71Sopenharmony_ci  }
177e41f4b71Sopenharmony_ci}
178e41f4b71Sopenharmony_ci```
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ciIf the target application is started, the App Linking configuration of the target application is correct.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci## FAQs
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci1. What should I do when the value of **skills** in the **Modules.json5** file of the application is incorrect?
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci   Ensure that the value of **host** is the domain name of the application.
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci2. What should I do when the developer website server is incorrectly configured?
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci   * Check the JSON configuration of the server and ensure that the value of **appIdentifier** is correct.
192e41f4b71Sopenharmony_ci   * 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.
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci3. What should I do when the system has not verified the domain name?
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci   After installing the application on the device, wait for at least 20 seconds to ensure that asynchronous verification is complete.
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci4. What is the mapping between applications and domain names?
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci   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.
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci5. If a domain name is associated with multiple applications, which application will be started by domain name?
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci   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.
205e41f4b71Sopenharmony_ci   
206e41f4b71Sopenharmony_ci   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.
207e41f4b71Sopenharmony_ci
208