文档管理中心
我的
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
指南与API参考指南媒体Scan Kit(统一扫码服务)接入“扫码直达”服务

接入“扫码直达”服务

说明

扫码直达能力仅支持中国境内(香港特别行政区、澳门特别行政区、中国台湾除外)接入使用。

在日常生活中,人们会使用各种应用扫各式各样的码,而“扫码直达”服务则为用户带来一种全新的扫码体验。

开发者将域名注册到“扫码直达”服务后,用户可通过控制中心等系统级的常驻入口,扫应用的二维码、条形码并跳转到应用对应服务页,实现一步直达服务的体验。

开发者接入“扫码直达”服务,能为应用带来:

  • 更浅层的扫码入口和更便捷的“扫码直达”服务体验。

  • HarmonyOS强大的扫码能力。

  • 更容易触达用户的全新渠道。

业务流程

  1. 开发者参考App Linking指导完成域名注册。

  2. 用户通过HarmonyOS扫码入口发起扫码请求。

  3. HarmonyOS扫码入口调用系统能力解析码值,查询码值对应的应用信息后拉起应用。

  4. 解析码值结果跳转应用服务页。

开发步骤

  1. 参考开发准备完成必要的准备工作。

  2. 处理接收到的码值,完成应用内页面跳转逻辑。

    收起
    自动换行
    深色代码主题
    复制
    1. import { UIAbility, Want } from '@kit.AbilityKit';
    2. import { BusinessError } from '@kit.BasicServicesKit';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
    4. import { router, window } from '@kit.ArkUI';
    5. export default class EntryAbility extends UIAbility {
    6. private page: string = 'pages/Index';
    7. private uiContext?: UIContext;
    8. // 冷启动场景通过onCreate回调获取码值信息
    9. onCreate(want: Want): void {
    10. hilog.info(0x0001, '[Scan Access]', 'Succeeded in getting want in onCreate');
    11. // 从want中获取传入的链接信息。如传入的url为:https://www.example.com/scan
    12. this.getRouterUri(want);
    13. }
    14. // 热启动场景通过onNewWant回调获取码值信息
    15. onNewWant(want: Want): void {
    16. hilog.info(0x0001, '[Scan Access]', 'Succeeded in getting want in onNewWant');
    17. // 从want中获取传入的链接信息
    18. this.getRouterUri(want);
    19. }
    20. onWindowStageCreate(windowStage: window.WindowStage): void {
    21. hilog.info(0x0001, '[Scan Access]', 'Ability onWindowStageCreate');
    22. try {
    23. windowStage.getMainWindow().then((windowObj: window.Window) => {
    24. try {
    25. windowStage.loadContent(this.page).then(() => {
    26. hilog.info(0x0001, '[Scan Access]', 'Succeeded in loading the content.');
    27. try {
    28. this.uiContext = windowObj.getUIContext();
    29. hilog.info(0x0001, '[Scan Access]', 'Succeeded in getting UIContext.');
    30. } catch (err) {
    31. hilog.error(0x0001, '[Scan Access]', `Failed to get UIContext by windowObj. Code: ${err.code}.`);
    32. }
    33. }).catch((err: BusinessError) => {
    34. hilog.error(0x0001, '[Scan Access]', `Failed to load the content. Code: ${err.code}.`);
    35. })
    36. } catch (err) {
    37. hilog.error(0x0001, '[Scan Access]', `Failed to load the content. Code: ${err.code}.`);
    38. }
    39. }).catch((err: BusinessError) => {
    40. hilog.error(0x0001, '[Scan Access]', `Failed to get MainWindow. Code: ${err.code}.`);
    41. })
    42. } catch (err) {
    43. hilog.error(0x0001, '[Scan Access]', `Failed to get MainWindow. Code: ${err.code}.`);
    44. }
    45. }
    46. // 解析扫码结果,跳转相应页面
    47. private getRouterUri(want: Want): void {
    48. const uri: string | undefined = want?.uri;
    49. if (!uri) {
    50. return;
    51. }
    52. this.page = 'pages/Access';
    53. if (this.uiContext) {
    54. // 开发者根据解析的uri跳转至相应页面,例如需要跳转页面:pages/Access
    55. const status: router.RouterState = this.uiContext.getRouter().getState();
    56. if (status && status.name !== 'Access') {
    57. try {
    58. // 根据uri参数做业务处理
    59. this.uiContext.getRouter().pushUrl({
    60. url: 'pages/Access'
    61. }).catch((err: BusinessError) => {
    62. hilog.error(0x0001, '[Scan Access]', `Failed to pushUrl by getRouter. Code: ${err.code}.`);
    63. });
    64. } catch (err) {
    65. hilog.error(0x0001, '[Scan Access]', `Failed to pushUrl by getRouter. Code: ${err.code}.`);
    66. }
    67. }
    68. }
    69. }
    70. }
  3. 验证“扫码直达”服务。

    1. 将配置好域名映射关系的测试应用安装到本地。
    2. 打开HarmonyOS扫码入口(控制中心扫码入口),扫描应用发行的二维码。
    3. 确认能否拉起应用并跳转目标服务页。

集成效果,以美团单车场景为例:

开发后验证

集成扫码直达能力应用用户体验质量建议

应用完成开发后,可参照以下标准检查集成扫码直达后的用户体验是否符合预期:

展开
标准编号 标准项名称 类型 标准详细描述
1 应用已安装跳转体验 规则 通过系统扫一扫扫描码图可以跳转到履约页面。履约页面指的是扫码后的目标服务页面,例如,扫支付码跳转到应用的支付页面,而非首页。
2 应用未安装跳转体验 建议 通过系统扫一扫扫描码图会拉起浏览器加载码值所对应的网页,请设计网页满足用户诉求、指导用户安装应用等。