
回复
作为鸿蒙生态开发者,App Linking是实现应用间无缝跳转的核心能力。
App Linking是HarmonyOS提供的安全应用跳转方案,相比传统Deep Link增加了域名校验机制,形成三层安全防护:
防护层 | 实现方式 | 安全价值 |
---|---|---|
域名校验 | 服务器端applinking.json文件验证 | 防止恶意域名伪造 |
APP ID绑定 | AGC控制台应用与域名关联 | 确保链接指向合法应用 |
链路加密 | HTTPS传输+参数签名 | 防止链路数据改 |
graph TD
A[用户点击链接] --> B[系统解析URL]
B --> C[域名校验模块]
C --> D{校验是否通过?}
D -- 是 --> E[打开目标应用]
D -- 否 --> F[跳转浏览器]
E --> G[应用接收URI处理]
AGC控制台配置
域名准备
harmony.example.com
)// 服务器根目录/.well-known/applinking.json
{
"app_id": "your_app_id",
"paths": [
{
"path": "/programs",
"target": "bundle:com.example.app"
}
]
}
import { UIAbility, Want } from '@ohos.app.ability.common';
import { url } from '@ohos.arkts';
export default class EntryAbility extends UIAbility {
onCreate(want: Want) {
const uri = want?.uri;
if (uri) {
const urlObject = url.URL.parseURL(uri);
const action = urlObject.params.get('action');
const programId = urlObject.params.get('id');
// 根据参数执行不同逻辑
this.handleDeepLink(action, programId);
}
}
private handleDeepLink(action: string, id: string) {
switch(action) {
case 'show':
this.router.pushUrl(`/pages/program/detail?id=${id}`);
break;
case 'buy':
this.router.pushUrl(`/pages/order/create?id=${id}`);
break;
default:
this.router.pushUrl('/pages/home');
}
}
}
import { common } from '@ohos.app.ability.common';
// 生成分享链接
function generateShareLink(programId: string) {
return `https://harmony.example.com/programs?action=show&id=${programId}`;
}
// 打开分享链接
async function openShareLink(link: string) {
const context = getContext(this) as common.UIAbilityContext;
const options: common.OpenLinkOptions = {
appLinkingOnly: false, // 允许fallback到浏览器
isNewTask: true // 新建任务栈
};
await context.openLink(link, options);
}
// 广告点击处理
function handleAdClick(adData: AdData) {
const link = `https://harmony.example.com/ads?action=redirect&adId=${adData.id}`;
const context = getContext(this) as common.UIAbilityContext;
context.openLink(link, {
appLinkingOnly: true,
extraInfo: {
trackingId: adData.trackingId,
source: 'ad_network'
}
}).then(() => {
// 记录广告点击
this.trackAdClick(adData.id);
}).catch((error) => {
// 处理跳转失败,如引导下载应用
this.promptDownloadApp();
});
}
// 后台接收链接处理
import { BackgroundAbility } from '@ohos.app.ability.background';
export default class BackgroundEntry extends BackgroundAbility {
onReceiveWant(want: Want) {
const uri = want?.uri;
if (uri) {
const urlObject = url.URL.parseURL(uri);
const action = urlObject.params.get('action');
// 静默处理逻辑,如更新数据
if (action === 'sync') {
this.syncDataFromLink(uri);
}
}
}
}
问题现象 | 可能原因 | 解决方案 |
---|---|---|
链接无法跳转 | 域名校验失败 | 检查applinking.json配置 |
跳转到浏览器 | 应用未安装或校验失败 | 提供应用下载引导 |
参数解析错误 | URL格式不正确 | 使用url.URL.parseURL标准化解析 |
跨设备跳转失败 | 设备间信任关系未建立 | 通过鸿蒙账号建立设备信任链 |
通过上述实践方案,开发者可以在HarmonyOS平台上构建安全可靠的跨应用跳转体验。App Linking不仅提升了用户体验,还通过严格的域名校验机制保障了链路安全,是鸿蒙生态应用开发的必备技能之一。在实际应用中,建议结合具体场景优化链接处理流程,持续监控跳转成功率,不断提升用户体验。