1# Using Deep Linking for Application Redirection 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @hanchen45; @Luobniz21--> 6<!--Designer: @ccllee1--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10In Deep Linking, the system, based on the passed-in URI, searches for the application that meets the conditions 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. 11 12## Working Principles 13 14Deep 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). 15 16 17## Procedure for the Target Application 18 19### Configuring the module.json5 File 20 21To be accessed by other applications, an application must configure the [skills](../quick-start/module-configuration-file.md#skills) field of the [module.json5 file](../quick-start/module-configuration-file.md). 22 23> **NOTE** 24> 25> By default, the **skills** field contains a skill object, which is used to identify the application entry. Application redirection links should not be configured in this object. Instead, separate skill objects should be used. If there are multiple redirection scenarios, create different skill objects under **skills**. Otherwise, the configuration does not take effect. 26> 27> In Deep Linking, the value of **scheme** should not start with **ohos**. Generally, it should also not use values like **https**, **http**, or **file**, which are already in use by system applications, as this may cause the link to match with the corresponding system application. 28 29 30A configuration example is as follows: 31 32```json 33{ 34 "module": { 35 // ... 36 "abilities": [ 37 { 38 // ... 39 "skills": [ 40 { 41 "entities": [ 42 "entity.system.home" 43 ], 44 "actions": [ 45 "ohos.want.action.home" 46 ] 47 }, 48 { 49 "actions": [ 50 // actions cannot be empty. Otherwise, matching the target application fails. 51 "ohos.want.action.viewData" 52 ], 53 "uris": [ 54 { 55 // scheme is mandatory and can be customized. The following uses link as an example. Replace it with the actual scheme. 56 "scheme": "link", 57 // host is mandatory. Configure the domain name to be matched. 58 "host": "www.example.com" 59 } 60 ] 61 } // Add a skill object for redirection. If there are multiple redirection scenarios, create multiple skill objects. 62 ] 63 } 64 ] 65 } 66} 67``` 68 69### Obtaining and Parsing the Link Passed by the Caller 70 71In the [onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#oncreate) or [onNewWant()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#onnewwant) lifecycle callback of the UIAbility of the target application, obtain and parse the link passed by the caller. 72 73```ts 74// EntryAbility.ets is used as an example. 75import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 76import { url } from '@kit.ArkTS'; 77 78export default class EntryAbility extends UIAbility { 79 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 80 // Obtain the input link information from want. 81 // For example, the input URL is link://www.example.com/programs?action=showall. 82 let uri = want?.uri; 83 if (uri) { 84 // Parse the query parameter from the link. You can perform subsequent processing based on service requirements. 85 let urlObject = url.URL.parseURL(want?.uri); 86 let action = urlObject.params.get('action'); 87 // For example, if action is set to showall, all programs are displayed. 88 if (action === "showall") { 89 // ... 90 } 91 } 92 } 93} 94``` 95 96## Implementing Application Redirection (Required for the Caller Application) 97 98The following uses three cases to describe how to use [openLink()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#openlink12) and [startAbility()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#startability) to implement application redirection and how to implement application redirection in the [Web component](../reference/apis-arkweb/arkts-basic-components-web.md). 99 100### Using openLink to Implement Application Redirection 101 102Pass in the URL of the target application into **link** of [openLink](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#openlink12), and set [appLinkingOnly](../reference/apis-ability-kit/js-apis-app-ability-openLinkOptions.md#openlinkoptions) in the **options** field to **false**. 103 104 105The sample code is as follows: 106 107```ts 108import { common, OpenLinkOptions } from '@kit.AbilityKit'; 109import { BusinessError } from '@kit.BasicServicesKit'; 110import { hilog } from '@kit.PerformanceAnalysisKit'; 111 112const TAG: string = '[UIAbilityComponentsOpenLink]'; 113const DOMAIN_NUMBER: number = 0xFF00; 114 115@Entry 116@Component 117struct Index { 118 build() { 119 Button('start link', { type: ButtonType.Capsule, stateEffect: true }) 120 .width('87%') 121 .height('5%') 122 .margin({ bottom: '12vp' }) 123 .onClick(() => { 124 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 125 let link: string = "link://www.example.com"; 126 let openLinkOptions: OpenLinkOptions = { 127 appLinkingOnly: false 128 }; 129 130 try { 131 context.openLink(link, openLinkOptions) 132 .then(() => { 133 hilog.info(DOMAIN_NUMBER, TAG, 'openLink success.'); 134 }).catch((err: BusinessError) => { 135 hilog.error(DOMAIN_NUMBER, TAG, `openLink failed. Code is ${err.code}, message is ${err.message}`); 136 }); 137 } catch (paramError) { 138 hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`); 139 } 140 }) 141 } 142} 143``` 144 145### Using startAbility() to Implement Application Redirection 146 147Pass in the target application's link into **want** of [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#startability), which then uses [implicit Want](explicit-implicit-want-mappings.md#matching-rules-of-implicit-want) to trigger application redirection. 148 149 150The sample code is as follows: 151 152```ts 153import { common, Want } from '@kit.AbilityKit'; 154import { BusinessError } from '@kit.BasicServicesKit'; 155import { hilog } from '@kit.PerformanceAnalysisKit'; 156 157const TAG: string = '[UIAbilityComponentsOpenLink]'; 158const DOMAIN_NUMBER: number = 0xFF00; 159 160@Entry 161@Component 162struct Index { 163 build() { 164 Button('start ability', { type: ButtonType.Capsule, stateEffect: true }) 165 .width('87%') 166 .height('5%') 167 .margin({ bottom: '12vp' }) 168 .onClick(() => { 169 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 170 let want: Want = { 171 uri: "link://www.example.com" 172 }; 173 174 try { 175 context.startAbility(want).then(() => { 176 hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.'); 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 start ability. Code is ${paramError.code}, message is ${paramError.message}`); 182 } 183 }) 184 } 185} 186``` 187 188### Using the Web Component to Implement Application Redirection 189 190You can use the **Web** component to implement application redirection in the callback function of [onLoadIntercept](../reference/apis-arkweb/arkts-basic-components-web-events.md#onloadintercept10). 191 192The sample code is as follows: 193 194```ts 195// index.ets 196import { webview } from '@kit.ArkWeb'; 197import { BusinessError } from '@kit.BasicServicesKit'; 198import { common } from '@kit.AbilityKit'; 199 200@Entry 201@Component 202struct WebComponent { 203 controller: webview.WebviewController = new webview.WebviewController(); 204 205 build() { 206 Column() { 207 Web({ src: $rawfile('index.html'), controller: this.controller }) 208 .onLoadIntercept((event) => { 209 const url: string = event.data.getRequestUrl(); 210 if (url === 'link://www.example.com') { 211 (this.getUIContext().getHostContext() as common.UIAbilityContext).openLink(url) 212 .then(() => { 213 console.log('openLink success'); 214 }).catch((err: BusinessError) => { 215 console.error('openLink failed, err:' + JSON.stringify(err)); 216 }); 217 return true; 218 } 219 // If true is returned, the loading is blocked. Otherwise, the loading is allowed. 220 return false; 221 }) 222 } 223 } 224} 225``` 226 227Frontend page code: 228```html 229// index.html 230<!DOCTYPE html> 231<html> 232<head> 233 <meta charset="UTF-8"> 234</head> 235<body> 236<h1>Hello World</h1> 237<!--Method 1: Bind window.open to implement redirection.-->; 238<button class="doOpenLink" onclick="doOpenLink()">Redirect to application 1</button> 239<!--Method 2: Use a hyperlink for redirection.-->; 240<a href="link://www.example.com">Redirect to application 2</a> 241</body> 242</html> 243<script> 244 function doOpenLink() { 245 window.open("link://www.example.com") 246 } 247</script> 248``` 249