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