【解决方案】
基于Deep Linking实现H5拉起应用时检测是否可以打开相关应用,已安装则拉起,未安装则打开应用市场进行下载。
1.在拉起方的module.json5文件中配置querySchemes字段,表示本应用可能会用到的scheme查询。
"module": {
"querySchemes": [
"hwtips",
],
}
2.在被拉起方的module.json文件中的skill字段中配置该应用支持的scheme协议,表示这个应用可以通过此协议打开,例如玩机技巧应用想被成功拉起则需要玩机技巧应用在工程的model.json5文件中配置abilities的skills中配置。
"skills": [
{
"actions": [
"ohos.want.action.viewData"
],
"uris": [
{
"scheme": "hwtips"
}
]
}
]
3.使用onLoadIntercept拦截前端请求,目的是获取拉起应用的url。
Web({ src: $rawfile('PullUp.html'), controller: this.controller })
.onLoadIntercept((event) => {
if (event) {
let url: string = event.data.getRequestUrl();
}
})
4.使用canOpenLink检测是否可以打开相关应用,可以打开则拉起应用。
if (canOpen) {
const WANT: Want = {
uri: url
}
const CONTEXT= getContext(this) as common.UIAbilityContext;
CONTEXT.startAbility(WANT).then(() => {
// 拉起成功
}).catch(() => {
})
}
5.若未安装,不可打开则跳转应用市场进行下载。
if (canOpen) {
} else {
promptAction.showDialog({
title: $r('app.string.tips'),
message: $r('app.string.download'),
buttons: [{
text: $r('app.string.confirm'),
color: $r('app.string.fontcolor'),
}]
});
.then(index => {
const WANT: Want = {
uri: `store://appgallery.huawei.com/app/detail?id=com.huawei.hmos.tips`
}
const CONTEXT = getContext(this) as common.UIAbilityContext;
CONTEXT.startAbility(WANT).then(() => {
})
})
}