• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Switching from Explicit Want Redirection to Linking Redirection
2
3Since API version 12, it is not recommended that third-party applications start other applications by specifying an ability (implicit Want mode). Instead, the [linking mode](app-startup-overview.md#application-links) is recommended.
4
5This section describes how to switch from explicit Want mode to linking mode.
6
7## Starting the UIAbility of Another Application
8
91. Install the application on your device. In the [module.json5 file](../quick-start/module-configuration-file.md) of the UIAbility, configure **entities**, **actions**, and **uri** under **skills**.
10    - The **actions** field must contain **ohos.want.action.viewData**.
11    - The **entities** field must contain **entity.system.browsable**.
12    - The **uris** field must contain an element whose **scheme** is **https**. **domainVerify** must be set to **true**. For details about the URI matching rules, see [Matching Rules of uri](explicit-implicit-want-mappings.md#matching-rules-of-uri). If **domainVerify** is set to **true**, domain name verification is enabled. In this case, the target application must pass domain name verification during App Linking. For details about how to configure the App Linking domain name, see App Linking.
13
14    ```json
15    {
16      "module": {
17        // ...
18        "abilities": [
19          {
20            // ...
21            "skills": [
22              {
23                "entities": [
24                  "entity.system.browsable"
25                ],
26                "actions": [
27                  "ohos.want.action.viewData"
28                ],
29                "uris": [
30                  {
31                    "scheme": "https",
32                    "host": "www.example.com",
33                  }
34                ],
35              "domainVerify": true
36              }
37            ]
38          }
39        ]
40      }
41    }
42    ```
43
442. Call [openLink](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextopenlink12) to trigger redirection. The redirected-to link and [options](../reference/apis-ability-kit/js-apis-app-ability-openLinkOptions.md) must be passed in, but the bundle name, module name, and ability name are not required. The system matches the application that meets the skills configuration based on the link.
45    - If **appLinkingOnly** in **options** is set to **true**, the target application must pass domain name verification (Internet connection required). A unique matching item or an unmatched result will be returned.
46    - If **appLinkingOnly** in **options** is set to **false**, the system preferentially attempts to start the target application in App Linking mode. If no matching application is found, the system starts the application in Deep Linking mode.
47
48    ```ts
49    import { common } from '@kit.AbilityKit';
50    import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
51    import { BusinessError } from '@ohos.base';
52    import hilog from '@ohos.hilog';
53
54    const TAG: string = '[UIAbilityComponentsOpenLink]';
55    const DOMAIN_NUMBER: number = 0xFF00;
56
57    @Entry
58    @Component
59    struct Index {
60      build() {
61        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
62          .width('87%')
63          .height('5%')
64          .margin({ bottom: '12vp' })
65          .onClick(() => {
66            let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
67            // When using startAbility to explicitly start other UIAbilities, the openLink API is recommended.
68            // let want: Want = {
69            //   bundleName: "com.test.example",
70            //   moduleName: "entry",
71            //   abilityName: "EntryAbility"
72            // };
73            // try {
74            //   context.startAbility(want)
75            //     .then(() => {
76            //       hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
77            //     }).catch((err: BusinessError) => {
78            //       hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
79            //     })
80            // } catch (paramError) {
81            //   hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
82            // }
83            let link: string = "https://www.example.com";
84            let openLinkOptions: OpenLinkOptions = {
85              // Specify whether the matched abilities options must pass App Linking domain name verification.
86              appLinkingOnly: true,
87              // Same as parameter in want, which is used to transfer parameters.
88              parameters: {demo_key: "demo_value"}
89            };
90
91            try {
92              context.openLink(link, openLinkOptions)
93                .then(() => {
94                  hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
95                }).catch((err: BusinessError) => {
96                  hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
97                })
98            } catch (paramError) {
99              hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
100            }
101          })
102      }
103    }
104    ```
105
106## Starting the UIAbility of Another Application and Obtaining the Return Result
107
1081. Install the application on your device. In the [module.json5 file](../quick-start/module-configuration-file.md) of the UIAbility, configure **entities**, **actions**, and **uri** under **skills**.
109
110    - The **actions** field must contain **ohos.want.action.viewData**.
111    - The **entities** field must contain **entity.system.browsable**.
112    - The **uris** field must contain an element whose **scheme** is **https**. **domainVerify** must be set to **true**. For details about the URI matching rules, see [Matching Rules of uri](explicit-implicit-want-mappings.md#matching-rules-of-uri). If **domainVerify** is set to **true**, domain name verification is enabled. In this case, the target application must pass domain name verification during App Linking. For details about how to configure the App Linking domain name, see App Linking.
113
114    ```json
115    {
116      "module": {
117        // ...
118        "abilities": [
119          {
120            // ...
121            "skills": [
122              {
123                "entities": [
124                  "entity.system.browsable"
125                ],
126                "actions": [
127                  "ohos.want.action.viewData"
128                ],
129                "uris": [
130                  {
131                    "scheme": "https",
132                    "host": "www.example.com",
133                  }
134                ],
135              "domainVerify": true
136              }
137            ]
138          }
139        ]
140      }
141    }
142    ```
143
1442. Call [openLink](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextopenlink12) to trigger redirection. The redirected-to link and [options](../reference/apis-ability-kit/js-apis-app-ability-openLinkOptions.md) must be passed in, but the bundle name, module name, and ability name are not required. The system matches the application that meets the skills configuration based on the link. **AbilityResult** is transferred to the callback function through input parameters and returned to the caller application when the ability is terminated. The startup success or failure result is returned through a promise.<br>
145    - If **appLinkingOnly** in **options** is set to **true**, the target application must pass domain name verification (Internet connection required). A unique matching item or an unmatched result will be returned.
146    - If **appLinkingOnly** in **options** is set to **false**, the system preferentially attempts to start the target application in App Linking mode. If no matching application is found, the system starts the application in Deep Linking mode.
147
148    ```ts
149    import { common } from '@kit.AbilityKit';
150    import OpenLinkOptions from '@ohos.app.ability.OpenLinkOptions';
151    import { BusinessError } from '@ohos.base';
152    import hilog from '@ohos.hilog';
153
154    const TAG: string = '[UIAbilityComponentsOpenLink]';
155    const DOMAIN_NUMBER: number = 0xFF00;
156
157    @Entry
158    @Component
159    struct Index {
160      build() {
161        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
162          .width('87%')
163          .height('5%')
164          .margin({ bottom: '12vp' })
165          .onClick(() => {
166            let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
167            // When using startAbility to explicitly start other UIAbilities, the openLink API is recommended.
168            // let want: Want = {
169            //   bundleName: "com.test.example",
170            //   moduleName: "entry",
171            //   abilityName: "EntryAbility"
172            // };
173            // try {
174            //   context.startAbilityForResult(want)
175            //     .then((data) => {
176            //       hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success. data:' + JSON.stringify(data));
177            //     }).catch((err: BusinessError) => {
178            //       hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
179            //     })
180            // } catch (paramError) {
181            //   hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
182            // }
183            let link: string = "https://www.example.com";
184            let openLinkOptions: OpenLinkOptions = {
185              // Specify whether the matched abilities options must pass App Linking domain name verification.
186              appLinkingOnly: true,
187              // Same as parameter in want, which is used to transfer parameters.
188              parameters: {demo_key: "demo_value"}
189            };
190
191            try {
192              context.openLink(link, openLinkOptions, (err, data) => {
193                // AbilityResult callback, which is triggered only when the started ability is terminated.
194                hilog.info(DOMAIN_NUMBER, TAG, 'open link success. Callback result:' + JSON.stringify(data));
195              }).then(() => {
196                hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
197              }).catch((err: BusinessError) => {
198                hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
199              })
200            } catch (paramError) {
201              hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
202            }
203          })
204      }
205    }
206    ```
207