1# Using Deep Linking for Application Redirection
2
3In Deep Linking, the system, based on the passed-in URI and [URI matching rules](explicit-implicit-want-mappings.md#matching-rules-of-uri), searches for the application that meets the URL skills configuration from the locally installed applications and starts that application. If multiple applications are matched, a dialog box is displayed for users to select one of them.
4
5## Working Principles
6
7Deep Linking searches for an application based on the URI matching rules in implicit Want mechanism and starts the matching application. For details about the URI matching rules of implicit Want, see [Matching Rules of uri](explicit-implicit-want-mappings.md#matching-rules-of-uri).
8
9
10## Declaring the URL Skills in the Configuration File (Required for the Target Application)
11
12To be accessed by other applications, an application must declare the URL skills in the [module.json5 file](../quick-start/module-configuration-file.md). The value of **scheme** under **uri** can be customized. It can be any string that does not contain special characters or start with **ohos**.
13
14A configuration example is as follows:
15
16```json
17{
18  "module": {
19    // ...
20    "abilities": [
21      {
22        // ...
23        "skills": [
24          {
25            "uris": [
26              {
27                // scheme is mandatory and can be customized. The following uses link as an example. Replace it with the actual scheme.
28                "scheme": "link",
29                // host is mandatory. Configure the domain name to be matched.
30                "host": "www.example.com",
31                // path is optional. To distinguish between applications that are associated with the same domain name, you are advised to configure this field.
32                "path": "path1"
33              }
34            ]
35          }
36        ]
37      }
38    ]
39  }
40}
41```
42
43
44## Implementing Application Redirection (Required for the Caller Application)
45
46The following uses two cases to describe how to use [openLink()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextopenlink12) and [startAbility()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to implement application redirection.
47
48### Using openLink() to Open a Browser Page
49
50Pass in the URL of the target application into **link** of [openLink()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextopenlink12), and set **appLinkingOnly** in the **options** field to **false**.
51
52
53The sample code is as follows:
54
55```ts
56import { common } from '@kit.AbilityKit';
57import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
58import { BusinessError } from '@ohos.base';
59import hilog from '@ohos.hilog';
60
61const TAG: string = '[UIAbilityComponentsOpenLink]';
62const DOMAIN_NUMBER: number = 0xFF00;
63
64@Entry
65@Component
66struct Index {
67  build() {
68    Button('start link', { type: ButtonType.Capsule, stateEffect: true })
69      .width('87%')
70      .height('5%')
71      .margin({ bottom: '12vp' })
72      .onClick(() => {
73        let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
74        let link: string = "link://www.example.com";
75        let openLinkOptions: OpenLinkOptions = {
76          appLinkingOnly: false
77        };
78
79        try {
80          context.openLink(link, openLinkOptions)
81            .then(() => {
82              hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
83            }).catch((err: BusinessError) => {
84              hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
85            })
86        } catch (paramError) {
87          hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
88        }
89      })
90  }
91}
92```
93
94### Using startAbility() to Implement Application Redirection
95
96Pass in the target application's link into **want** of [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability), which then uses [implicit Want](explicit-implicit-want-mappings.md#matching-rules-of-implicit-want) to trigger application redirection. In addition, you must pass in the **action** and **entity** fields to be matched.
97
98
99The sample code is as follows:
100
101```ts
102import { common, Want } from '@kit.AbilityKit';
103import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
104import { BusinessError } from '@ohos.base';
105import hilog from '@ohos.hilog';
106
107const TAG: string = '[UIAbilityComponentsOpenLink]';
108const DOMAIN_NUMBER: number = 0xFF00;
109
110@Entry
111@Component
112struct Index {
113  build() {
114    Button('start ability', { type: ButtonType.Capsule, stateEffect: true })
115      .width('87%')
116      .height('5%')
117      .margin({ bottom: '12vp' })
118      .onClick(() => {
119        let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
120        let want: Want = {
121            uri: "link://www.example.com"
122        };
123
124        try {
125          context.startAbility(want).then(() => {
126            hilog.info(DOMAIN_NUMBER, TAG, 'start ability success.');
127          }).catch((err: BusinessError) => {
128            hilog.error(DOMAIN_NUMBER, TAG, `start ability failed. Code is ${err.code}, message is ${err.message}`);
129          })
130        } catch (paramError) {
131          hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${paramError.code}, message is ${paramError.message}`);
132        }
133      })
134  }
135}
136```
137