1# (可选)使用canOpenLink判断应用是否可访问 2<!--Kit: Ability Kit--> 3<!--Subsystem: BundleManager--> 4<!--Owner: @wanghang904--> 5<!--Designer: @hanfeng6--> 6<!--Tester: @kongjing2--> 7<!--Adviser: @Brilliantry_Rui--> 8 9## 使用场景 10在应用A想要拉起应用B的场景中,应用A可先调用canOpenLink接口判断应用B是否可访问,如果可访问,再拉起应用B。 11 12> **说明:** 13> 14> canOpenLink接口不支持判断以App Linking方式跳转的目标应用是否安装。 15 16## 约束限制 17在entry模块的module.json5文件中的[querySchemes](../quick-start/module-configuration-file.md)字段中,最多允许配置50个URL scheme。 18## 接口说明 19canOpenLink是[bundleManager](../reference/apis-ability-kit/js-apis-bundleManager.md#bundlemanagercanopenlink12)提供的支持判断目标应用是否可访问的接口。 20匹配规则请参考[显式Want与隐式Want匹配规则](explicit-implicit-want-mappings.md)。 21## 操作步骤 22### 调用方操作步骤 23 241. 在entry模块的module.json5文件中配置[querySchemes](../quick-start/module-configuration-file.md)属性,声明想要查询的URL scheme。 25 26 ```json 27 { 28 "module": { 29 //... 30 "querySchemes": [ 31 "app1Scheme" 32 ] 33 } 34 } 35 ``` 36 372. 导入ohos.bundle.bundleManager模块。 383. 调用canOpenLink接口。 39 40 ```ts 41 import { bundleManager } from '@kit.AbilityKit'; 42 import { BusinessError } from '@kit.BasicServicesKit'; 43 import { hilog } from '@kit.PerformanceAnalysisKit'; 44 try { 45 let link = 'app1Scheme://test.example.com/home'; 46 let canOpen = bundleManager.canOpenLink(link); 47 hilog.info(0x0000, 'testTag', 'canOpenLink successfully: %{public}s', JSON.stringify(canOpen)); 48 } catch (err) { 49 let message = (err as BusinessError).message; 50 hilog.error(0x0000, 'testTag', 'canOpenLink failed: %{public}s', message); 51 } 52 ``` 53 54### 目标方操作步骤 55在module.json5文件中配置[uris](../quick-start/module-configuration-file.md#skills标签)属性。 56 57```json 58{ 59 "module": { 60 //... 61 "abilities": [ 62 { 63 //... 64 "skills": [ 65 { 66 // actions不能为空,actions为空会造成目标方匹配失败 67 "actions": ["ohos.want.action.home"], 68 "uris": [ 69 { 70 "scheme": "app1Scheme", 71 "host": "test.example.com", 72 "pathStartWith": "home" 73 } 74 ] 75 } 76 ] 77 } 78 ] 79 } 80} 81``` 82 83## FAQ 841. 为什么querySchemes中最多允许配置50个URL scheme? 85 86 canOpenLink()接口提供了判断应用是否可以访问的能力。通过该能力,应用可以间接获取到指定应用是否安装等信息。 87 88 为了保护系统安全和用户隐私,避免恶意应用扫描应用安装列表等行为,要求开发者在使用canOpenLink()接口时必须配置querySchemes属性,且最多允许配置50个URL scheme。 89