• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# (可选)使用canOpenLink判断应用是否可访问
2## 使用场景
3在应用A想要拉起应用B的场景中,应用A可先调用canOpenLink接口判断应用B是否可访问,如果可访问,再拉起应用B。
4## 约束限制
5在entry模块的module.json5文件中的[querySchemes](../quick-start/module-configuration-file.md)字段中,最多允许配置50个URL scheme。
6## 接口说明
7canOpenLink是[bundleManager](../reference/apis-ability-kit/js-apis-bundleManager.md#bundlemanagercanopenlink12)提供的支持判断目标应用是否可访问的接口。
8匹配规则请参考[显式Want与隐式Want匹配规则](explicit-implicit-want-mappings.md)。
9## 操作步骤
10### 调用方操作步骤
11
121. 在entry模块的module.json5文件中配置[querySchemes](../quick-start/module-configuration-file.md)属性,声明想要查询的URL scheme。
13
14    **配置示例**
15    ```json
16    {
17      "module": {
18        //...
19        "querySchemes": [
20          "app1Scheme"
21        ]
22      }
23    }
24    ```
25
262. 导入ohos.bundle.bundleManager模块。
273. 调用canOpenLink接口。
28
29    **示例**
30    ```ts
31    import { bundleManager } from '@kit.AbilityKit';
32    import { BusinessError } from '@kit.BasicServicesKit';
33    import { hilog } from '@kit.PerformanceAnalysisKit';
34    try {
35      let link = 'app1Scheme://test.example.com/home';
36      let canOpen = bundleManager.canOpenLink(link);
37      hilog.info(0x0000, 'testTag', 'canOpenLink successfully: %{public}s', JSON.stringify(canOpen));
38    } catch (err) {
39      let message = (err as BusinessError).message;
40      hilog.error(0x0000, 'testTag', 'canOpenLink failed: %{public}s', message);
41    }
42    ```
43
44### 目标方操作步骤
45module.json5文件中配置[uris](../quick-start/module-configuration-file.md#skills标签)属性。
46
47**配置示例**
48```json
49{
50  "module": {
51    //...
52    "abilities": [
53      {
54        //...
55        "skills": [
56          {
57            "uris": [
58              {
59                "scheme": "app1Scheme",
60                "host": "test.example.com",
61                "pathStartWith": "home"
62              }
63            ]
64          }
65        ]
66      }
67    ]
68  }
69}
70```
71