• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Switching from Explicit Want Redirection to Linking Redirection
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: Ability-->
5<!--Owner: @hanchen45; @Luobniz21-->
6<!--Designer: @ccllee1-->
7<!--Tester: @lixueqing513-->
8<!--Adviser: @huipeizi-->
9
10Starting from 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.
11
12This section describes how to switch from explicit Want mode to linking mode.
13
14## Starting the UIAbility of Another Application
15
161. 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**.
17    - The **actions** field must contain **ohos.want.action.viewData**.
18    - The **entities** field must contain **entity.system.browsable**.
19    - 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.
20
21    ```json
22    {
23      "module": {
24        // ...
25        "abilities": [
26          {
27            // ...
28            "skills": [
29              {
30                "entities": [
31                  "entity.system.browsable"
32                ],
33                "actions": [
34                  "ohos.want.action.viewData"
35                ],
36                "uris": [
37                  {
38                    "scheme": "https",
39                    "host": "www.example.com"
40                  }
41                ],
42              "domainVerify": true
43              }
44            ]
45          }
46        ]
47      }
48    }
49    ```
50
512. Call [openLink](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#openlink12) to trigger redirection. The 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.
52    - 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.
53    - 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.
54
55    ```ts
56    import { common, OpenLinkOptions } from '@kit.AbilityKit';
57    import { BusinessError } from '@kit.BasicServicesKit';
58    import { hilog } from '@kit.PerformanceAnalysisKit';
59
60    const TAG: string = '[UIAbilityComponentsOpenLink]';
61    const DOMAIN_NUMBER: number = 0xFF00;
62
63    @Entry
64    @Component
65    struct Index {
66      build() {
67        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
68          .width('87%')
69          .height('5%')
70          .margin({ bottom: '12vp' })
71          .onClick(() => {
72            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
73            // When using startAbility to explicitly start other UIAbilities, the openLink API is recommended.
74            // let want: Want = {
75            //   bundleName: "com.test.example",
76            //   moduleName: "entry",
77            //   abilityName: "EntryAbility"
78            // };
79            // try {
80            //   context.startAbility(want)
81            //     .then(() => {
82            //       hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
83            //     }).catch((err: BusinessError) => {
84            //       hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
85            //     })
86            // } catch (paramError) {
87            //   hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
88            // }
89            let link: string = "https://www.example.com";
90            let openLinkOptions: OpenLinkOptions = {
91              // Specify whether the matched abilities options must pass App Linking domain name verification.
92              appLinkingOnly: true,
93              // Same as parameter in want, which is used to transfer parameters.
94              parameters: {demo_key: "demo_value"}
95            };
96
97            try {
98              context.openLink(link, openLinkOptions)
99                .then(() => {
100                  hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
101                }).catch((err: BusinessError) => {
102                  hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
103                })
104            } catch (paramError) {
105              hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
106            }
107          })
108      }
109    }
110    ```
111
112## Starting the UIAbility of Another Application and Obtaining the Return Result
113
1141. 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**.
115
116    - The **actions** field must contain **ohos.want.action.viewData**.
117    - The **entities** field must contain **entity.system.browsable**.
118    - 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.
119
120    ```json
121    {
122      "module": {
123        // ...
124        "abilities": [
125          {
126            // ...
127            "skills": [
128              {
129                "entities": [
130                  "entity.system.browsable"
131                ],
132                "actions": [
133                  "ohos.want.action.viewData"
134                ],
135                "uris": [
136                  {
137                    "scheme": "https",
138                    "host": "www.example.com"
139                  }
140                ],
141              "domainVerify": true
142              }
143            ]
144          }
145        ]
146      }
147    }
148    ```
149
1502. Call [openLink](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#openlink12) to trigger redirection. The 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 UIAbility is terminated. The startup success or failure result is returned through a promise.<br>
151    - 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.
152    - 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.
153
154    ```ts
155    import { common, OpenLinkOptions } from '@kit.AbilityKit';
156    import { BusinessError } from '@kit.BasicServicesKit';
157    import { hilog } from '@kit.PerformanceAnalysisKit';
158
159    const TAG: string = '[UIAbilityComponentsOpenLink]';
160    const DOMAIN_NUMBER: number = 0xFF00;
161
162    @Entry
163    @Component
164    struct Index {
165      build() {
166        Button('start link', { type: ButtonType.Capsule, stateEffect: true })
167          .width('87%')
168          .height('5%')
169          .margin({ bottom: '12vp' })
170          .onClick(() => {
171            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
172            // When using startAbility to explicitly start other UIAbilities, the openLink API is recommended.
173            // let want: Want = {
174            //   bundleName: "com.test.example",
175            //   moduleName: "entry",
176            //   abilityName: "EntryAbility"
177            // };
178            // try {
179            //   context.startAbilityForResult(want)
180            //     .then((data) => {
181            //       hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success. data:' + JSON.stringify(data));
182            //     }).catch((err: BusinessError) => {
183            //       hilog.error(DOMAIN_NUMBER, TAG, `startAbility failed. Code is ${err.code}, message is ${err.message}`);
184            //     })
185            // } catch (paramError) {
186            //   hilog.error(DOMAIN_NUMBER, TAG, `Failed to startAbility. Code is ${paramError.code}, message is ${paramError.message}`);
187            // }
188            let link: string = "https://www.example.com";
189            let openLinkOptions: OpenLinkOptions = {
190              // Specify whether the matched abilities options must pass App Linking domain name verification.
191              appLinkingOnly: true,
192              // Same as parameter in want, which is used to transfer parameters.
193              parameters: {demo_key: "demo_value"}
194            };
195
196            try {
197              context.openLink(link, openLinkOptions, (err, data) => {
198                // AbilityResult callback function, which is triggered only when the started UIAbility is terminated.
199                hilog.info(DOMAIN_NUMBER, TAG, 'open link success. Callback result:' + JSON.stringify(data));
200              }).then(() => {
201                hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
202              }).catch((err: BusinessError) => {
203                hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
204              })
205            } catch (paramError) {
206              hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
207            }
208          })
209      }
210    }
211    ```
212