🔒 HarmonyOS App Linking实战:HTTPS安全跳转全指南

一、App Linking核心优势

1. 安全三重保障

  • 域名校验:通过服务器applinking.json文件验证域名归属
  • HTTPS加密:所有链接使用HTTPS协议传输
  • 防伪造机制:非法域名跳转自动导向浏览器

2. 跨场景能力

场景 应用示例 优势
扫码直达 扫码打开商品详情 防恶意链接
社交分享 分享应用内页面到社交平台 已安装直接打开,未安装跳转下载页
广告引流 点击广告直达应用内活动 精准追踪来源

二、接入四步流程

1. AGC控制台开通服务

  1. 登录AGC进入「增长 > App Linking」开通服务
  2. 记录应用的APP ID(在「项目设置 > 常规」中)

2. 服务器配置验证文件

  • 在域名服务器创建.well-known/applinking.json
  • 文件内容示例:
    {
      "app_id": "your_app_id_here"
    }
    

3. AGC关联域名

  1. 在AGC填写应用的网址域名(如harmony.example.com
  2. 开启域名校验并发布,等待系统验证

4. 应用内处理链接

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 urlObj = url.URL.parseURL(uri);
      const action = urlObj.params.get('action');
      switch(action) {
        case 'product': this.openProductDetail(urlObj.params.get('id'));
        case 'event': this.openEventPage(urlObj.params.get('eventId'));
      }
    }
  }
}

三、安全跳转实现

1. 发起App Linking跳转

import { common } from '@ohos.app.ability.common';

const context = this.getContext(this) as common.UIAbilityContext;
const link = 'https://harmony.example.com/shop/product?id=123';
const options = {
  appLinkingOnly: true, // 强制使用App Linking
  openInBrowser: true   // 域名校验失败时跳转浏览器
};
context.openLink(link, options);

2. 未安装处理逻辑

系统会自动处理未安装情况:

  1. 域名校验通过但应用未安装 → 跳转应用市场下载页
  2. 域名校验失败 → 直接打开浏览器访问链接

四、实战场景示例

1. 扫码直达商品页

// 生成带参数的App Linking
const generateProductLink = (productId: string) => {
  return `https://harmony.example.com/product?id=${productId}&action=view`;
};

// 扫码后跳转
context.openLink(generateProductLink('P001'), { appLinkingOnly: true });

2. 社交分享带参页面

// 分享带参链接
const shareLink = 'https://harmony.example.com/event?eid=567&from=social';
shareToWeChat(shareLink);

// 应用内处理分享链接
onCreate(want) {
  const eid = url.URL.parseURL(want.uri).params.get('eid');
  this.loadEventDetail(eid);
}

五、开发注意事项

  1. 域名配置

    • 必须使用HTTPS协议
    • 验证文件需放在服务器根目录的.well-known
  2. 参数安全

    • 敏感参数建议加密后放在URL中
    • 优先使用URL Query而非Path传递参数
  3. 兼容性

    • API 12及以上版本支持
    • 旧版本设备自动降级为普通Deep Linking

总结

App Linking通过域名校验和HTTPS加密,实现了安全可靠的应用间跳转。开发者只需四步配置(AGC开通→服务器验证→域名关联→应用内处理),即可在扫码、分享等场景中提供安全的一键直达体验,同时自动处理未安装场景,提升用户转化率和安全性。

posted @ 2025-06-26 23:22  lyc233333  阅读(20)  评论(0)    收藏  举报