文章目录
- App Linking 和 DeepLink 的本质区别
- 核心代码
- `openLink` 的 `appLinkingOnly` 参数
- 绑定到 UI 点击事件
- 两种方案完整对比
- 一个容易踩的坑
- 版本要求
- 写在最后
上一篇聊了 DeepLink 方案,用的是store://私有协议。这篇来说说另一种方式:App Linking。
两种方式最终效果一样,都能把用户带到应用市场的写评论页,但底层逻辑完全不同。搞清楚这个差别,你才能在不同场景里选对方案。
App Linking 和 DeepLink 的本质区别
DeepLink 走的是私有 URI 协议(store://),本质上是个系统内部的跳转约定,出了华为生态就没用了。
App Linking 走的是标准HTTPS 链接,和你在浏览器里打开一个网页没什么两样——系统先尝试用已安装的应用打开,打不开就降级到浏览器。这种方式的好处是可移植性更强,同样的链接可以在网页、通知、分享卡片里到处用。
华为应用市场的 App Linking 地址格式:
https://appgallery.huawei.com/app/detail?id=<包名>&action=write-review跟 DeepLink 的 URI 参数部分完全一致,只是把store://换成了标准的https://。
核心代码
import{BusinessError}from'@kit.BasicServicesKit';import{hilog}from'@kit.PerformanceAnalysisKit';importtype{common}from'@kit.AbilityKit';constTAG='StartAppGalleryDetailAbilityView';// 在你的 @Component 中privatecontext:common.UIAbilityContext=this.getUIContext().getHostContext()ascommon.UIAbilityContext;startDetailWithAppLinking(bundleName:string):void{letlink:string=`https://appgallery.huawei.com/app/detail?id=${bundleName}&action=write-review`;this.context.openLink(link,{appLinkingOnly:false}).then(()=>{hilog.info(0x0001,TAG,`Succeeded in starting AppLinking successfully.`);}).catch((error:BusinessError)=>{hilog.error(0x0001,TAG,`Failed to start AppLinking. Code:${error.code}, message is${error.message}`);});}跟 DeepLink 最明显的区别是调用方法变了:startAbility→openLink。
openLink的appLinkingOnly参数
这个参数很关键,稍微解释一下:
this.context.openLink(link,{appLinkingOnly:false})appLinkingOnly: true:严格模式,只有目标应用显式配置了 App Linking 支持才能跳转,否则直接报错appLinkingOnly: false:宽松模式,优先走 App Linking,失败了会降级到浏览器或其他能处理 HTTPS 的应用
华为应用市场已经完整支持 App Linking,所以设false就好,给自己留条退路,也让跳转成功率更高。
绑定到 UI 点击事件
跟 DeepLink 方案一样,通常挂在设置页的点击事件上:
Row(){Text('五星好评').fontSize(14).fontWeight(FontWeight.Medium)Image($r('app.media.advanceIcon')).width(6.74).height(12.81)}.width('85%').height(48).justifyContent(FlexAlign.SpaceBetween).onClick(()=>{this.startDetailWithAppLinking('com.huawei.hmos.vmall');});实际项目中把包名换成你自己的就行。
两种方案完整对比
用了这两个方案之后,总结了一下各自适合的场景:
| 场景 | 推荐方案 |
|---|---|
| 纯鸿蒙应用,国内用户为主 | DeepLink(更直接) |
| 需要从网页/通知/消息卡片跳转 | App Linking(兼容性好) |
| 同一个链接复用到多个入口 | App Linking(HTTPS 链接通用) |
| 对跳转速度有要求 | DeepLink(少一层 URL 路由) |
说实话,纯在应用内触发的话两种方式体验差不多,效果也一样。如果你的产品有分享功能,用户可能从多渠道进入,App Linking 会更灵活一点。
一个容易踩的坑
openLink要求传入的链接必须是合法的 HTTPS 格式,不能用 HTTP,也不能用自定义协议头(那是 DeepLink 的活)。如果你误传了store://格式的 URI,会直接报错。
// 错误:openLink 不接受非 HTTPS 链接this.context.openLink('store://appgallery.huawei.com/...',{appLinkingOnly:false});// 正确this.context.openLink('https://appgallery.huawei.com/...',{appLinkingOnly:false});另外,openLink是从 API 11 开始才有的,比startAbility晚。不过我们这个项目要求 API 20,完全没问题。
版本要求
- API Version 20 Release 及以上
- HarmonyOS 6.0.0 Release SDK 及以上
- DevEco Studio 6.0.0 Release 及以上
写在最后
DeepLink 和 App Linking 本质上是一对互补的方案,不用纠结选哪个,按场景来就好。
如果你的"评分"功能只在应用内出现,两个随便选。如果以后要把这个链接放到推送通知、短信或者 H5 页面里用,App Linking 的 HTTPS 格式明显更合适。
下一篇会介绍第三种方案:直接在应用内弹出一个评分弹窗,不跳出去,用户体验更流畅。