HarmonyOS7 Deep Link:App Linking 让你的应用跳转无处不在

发布时间:2026/7/21 7:09:04
HarmonyOS7 Deep Link:App Linking 让你的应用跳转无处不在 文章目录前言Deep Link 到底是什么scheme 配置module.json5 里的关键代码App Linking 创建从服务器到 AGC跨应用跳转代码实现处理入参Intent 解析URI 格式规范写在最后前言说实话我第一次听说 Deep Link 的时候还一脸懵——这不就是个 URL 跳转吗直到做电商项目产品经理来了一句用户从微信点链接要直接跳到商品详情页我才发现这玩意儿真不是随便配个 scheme 就完事的。Deep Link 配不好用户点链接跳不到指定页面白白的流量就浪费了。今天把 Deep Link 和 App Linking 的配置到落地一次性讲清楚。Deep Link 到底是什么说白了Deep Link 就是通过 URI 把用户直接送到应用的某个页面。传统的方式用户点个链接先打开浏览器再引导下载 App装完打开还得到首页自己找内容。Deep Link 直接一步到位点链接 → 拉起 App → 跳到目标页面。HarmonyOS 里有两套方案Deep Linking用自定义 scheme比如myapp://简单但安全性差App Linking用 HTTPS 域名有域名校验更安全也更智能划重点App Linking 是 Deep Linking 的升级版官方推荐用 App Linking。scheme 配置module.json5 里的关键代码不管是 Deep Linking 还是 App Linking一切从 module.json5 的配置开始。Deep Linking 的配置长这样{ module: { abilities: [ { name: EntryAbility, exported: true, skills: [ { entities: [entity.system.browsable], actions: [ohos.want.action.viewData], uris: [ { scheme: myshop, host: www.myshop.com, path: goods } ] } ] } ] } }关键代码讲解entities必须包含entity.system.browsable表示这个 Ability 可以被浏览器类 Intent 拉起actions必须包含ohos.want.action.viewData声明要处理查看数据的动作uris里scheme是你自定义的协议名host是域名path是路径限定注意scheme用自定义的就行但不能跟系统保留的冲突比如http、https、file这些别碰。如果用 App Linkingscheme 必须改成https还要加domainVerify{ uris: [ { scheme: https, host: www.myshop.com, path: goods, domainVerify: true } ] }domainVerify: true是 App Linking 的灵魂——系统会校验你的域名所有权只有域名主人才能声明这个链接归谁App Linking 的 scheme 强制https不能自定义App Linking 创建从服务器到 AGCApp Linking 不像 Deep Linking 改改配置文件就行它需要三步配置服务器 → AGC → 客户端。第一步服务器放校验文件在你的域名服务器根目录创建.well-known/applinking.json{applinking:{apps:[{appIdentifier:1234567,index:1}]}}appIdentifier是 AGC 上的 APP ID在项目设置 应用信息里拿index是优先级同一域名绑多个 App 时index 大的优先被拉起文件路径必须是https://你的域名/.well-known/applinking.json不能换第二步AGC 配置登录 AppGallery Connect → 增长 → App Linking → 应用链接 → 创建填入你的域名。注意域名不带/https://www.example.com/是错的要写https://www.example.com。AGC 会去你服务器拉那个 JSON 做校验匹配就发布成功。发布后系统每 24 小时会重新拉取校验。第三步就是上面写的 module.json5 配置三步缺一不可。跨应用跳转代码实现配置好之后代码里怎么跳用openLink就行import{common,OpenLinkOptions}fromkit.AbilityKit;import{BusinessError}fromkit.BasicServicesKit;functionjumpToGoods(context:common.UIAbilityContext,goodsId:string):void{letlinkhttps://www.myshop.com/goods?actionshowGoodsgoodsId${goodsId};letoptions:OpenLinkOptions{appLinkingOnly:true};try{context.openLink(link,options).then((){console.info(App Linking 拉起成功);}).catch((err:BusinessError){console.error(拉起失败:${err.code},${err.message});});}catch(paramError){console.error(参数错误:${paramError.code});}}appLinkingOnly: true表示只走 App Linking 通道不走 Deep Linking如果拉起失败看错误码定位1234567这种一般是配置问题2234567一般是域名校验没过处理入参Intent 解析跳过来了目标页面得接住参数。在UIAbility的onCreate和onNewWant里解析import{AbilityConstant,UIAbility,Want}fromkit.AbilityKit;import{url}fromkit.ArkTS;exportdefaultclassEntryAbilityextendsUIAbility{onCreate(want:Want,launchParam:AbilityConstant.LaunchParam):void{this.handleLink(want);}onNewWant(want:Want,launchParam:AbilityConstant.LaunchParam):void{this.handleLink(want);}privatehandleLink(want:Want):void{leturiwant?.uri;if(!uri){return;}try{leturlObjurl.URL.parseURL(uri);letactionurlObj.params.get(action);letgoodsIdurlObj.params.get(goodsId);if(actionshowGoodsgoodsId){AppStorage.setOrCreate(goodsId,goodsId);}}catch(error){console.error(链接解析失败:${error});}}}关键代码讲解onCreate处理冷启动App 没在后台onNewWant处理热启动App 还活着两个都要写url.URL.parseURL(uri)把 URI 解析成结构化对象query 参数通过params.get()取解析完用AppStorage.setOrCreate传给页面页面侧用StorageProp或StorageLink接收URI 格式规范把 URI 的组成拆开看看组成部分说明示例scheme协议名https或自定义myshophost域名www.myshop.compath路径/goodsquery参数actionshowGoodsgoodsId123完整 URI拼接结果https://www.myshop.com/goods?actionshowGoodsgoodsId123module.json5 里path的匹配方式还有三种变体字段匹配方式示例path精确匹配/goods只匹配/goodspathStartWith前缀匹配/goods/匹配/goods/123pathRegex正则匹配goods/\d匹配/goods/456不配path的话这个域名的所有链接都会被你的 App 拦截慎用。写在最后Deep Link 这东西配置繁琐但价值巨大。尤其做社交分享、短信营销、扫码直达这些场景没 Deep Link 基本没法玩。我的建议是新项目直接上 App Linking别用 Deep Linking 的自定义 scheme 了。scheme 冲突、安全性差、没装 App 就歇菜——这些坑 App Linking 全帮你避了。唯一要耐住性子的是那个三步配置流程服务器 → AGC → 客户端一步都不能跳。配完之后记得用hdc shell hidumper -s AppDomainVerifyManager查一下校验状态success了才算真正生效。