HarmonyOS鸿蒙Next 5、6设备H5使用schemeURL跳转小程序失败

HarmonyOS鸿蒙Next 5、6设备H5使用schemeURL跳转小程序失败 鸿蒙4设备可以正常自动跳转

线上:鸿蒙5、6设备在h5页面自动跳转时:window.location.href = 【schemeURL】;网址直接改为此schemeURL地址,没有拉起小程序。

开发中测试设备h5获取的:UserAgent:Mozilla/5.0 (Phone;OpenHarmony 6.0; Android 10)AppleWebKit/537.36(KHTML, like Gecko)Chrome/132.0.0.0 Safari/537.36ArkWeb/6.0.0.120 MobileHuaweiBrowser/5.1.11.312data 测试中这个设备是可以自动跳转到小程序的。

这种情况是鸿蒙设备哪里的问题


更多关于HarmonyOS鸿蒙Next 5、6设备H5使用schemeURL跳转小程序失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

开发者你好,H5使用URL Scheme跳转小程序可以先通过getRequestUrl获取请求的URL再使用Deep Linking实现应用间跳转或者Want方式拉起小程序,具体实现可以参考此H5页面跳转微信小程序示例。(链接来自gitee)

【背景知识】

目前主流跳转微信小程序的方案有两种:

【解决方案】

通过SchemeURL拉起:

  1. 小程序后端生成加密的SchemeURL;
  2. 引入生成的SchemeURL,使用Deep Linking实现应用间跳转

示例代码:

import { common, OpenLinkOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          // t值由云侧生成
          let link: string = "";
          const context = getContext(this) as common.UIAbilityContext;
          let openLinkOptions: OpenLinkOptions = {
            appLinkingOnly: false,
          };
          try {
            context.openLink(link, openLinkOptions)
              .then(() => {
              }).catch((err: BusinessError) => {
            })
          } catch (paramError) {
          }
        })
    }.width('100%')
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next 5、6设备H5使用schemeURL跳转小程序失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


鸿蒙5以后是 HarmonyOS Next 了,不再是安卓。

Web组件支持通过URL Scheme跳转到其它App。开发者可以通过Web组件的onLoadIntercept回调拦截默认跳转逻辑,并在其中使用Deep Linking或App Linking的方式自定义跳转逻辑完成应用跳转。

另外,需要注意的是也无法跳转到通过卓易通安装的安卓应用。

鸿蒙5.0开始,需要web容器拦截到scheme进行跳转处理

import { webview } from '@kit.ArkWeb';
import { common, OpenLinkOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  build() {
    Column() {
      Web({
        src:
        "url地址",
        controller: this.controller
      })
        .onLoadIntercept((event) => {
          // 支付宝或者微信小程序的scheme
          if (event.data.getRequestUrl().startsWith('alipays://')) {
            this.startAlipay(event.data.getRequestUrl());
            return true;
          }
          return false;
        })
        .domStorageAccess(true)
        .height('100%')
        .width('100%')
    }
    .height('100%')
    .width('100%')
  }
  startAlipay(url: string) {
    let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
    let openLinkOptions: OpenLinkOptions = {
      appLinkingOnly: false,
    };
    try {
      context.openLink(url,openLinkOptions, (err, data) => {
      }).then(() => {
        console.info('open link success.');
      }).catch((err: BusinessError) => {
        console.error(`open link failed. Code is ${err.code}, message is ${err.message}`);
      })
    } catch (paramError) {
      console.error(`Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
    }
  }
}

在HarmonyOS Next 5、6设备上,H5页面通过schemeURL跳转小程序失败,通常是由于系统对scheme URL跳转的安全策略限制所致。鸿蒙Next版本强化了应用间跳转的权限管理,需要确保目标小程序已正确声明并配置了相应的URI Scheme,且H5页面所在的WebView已获得必要的跳转授权。此外,需检查scheme URL的格式是否符合鸿蒙平台的规范要求。

根据您描述的现象,这很可能是HarmonyOS Next(5.0/6.0)在ArkWeb引擎中对window.location.href方式触发scheme URL跳转的机制进行了调整或限制。

核心问题分析:

在HarmonyOS 4及更早版本(通常基于AOSP WebView)上,通过window.location.href设置一个scheme URL(如weixin://或您业务特定的scheme)是触发应用间跳转的常见且有效的方法。

然而,在HarmonyOS Next(5.0/6.0)中,系统使用了全新的ArkWeb引擎。出于安全性、用户体验和规范性的考虑,ArkWeb可能对通过window.location.href进行非HTTP/HTTPS的scheme导航施加了更严格的限制。直接赋值可能导致scheme被浏览器内核拦截或忽略,而不会传递给系统级的事件处理机制(如应用间拉起)。

关键差异点: 您提供的UserAgent信息中ArkWeb/6.0.0.120是关键。测试设备能跳转,而线上设备失败,可能的原因有:

  1. ArkWeb版本差异:线上用户设备的ArkWeb版本可能较早,存在已知的限制或Bug。您测试设备的版本(6.0.0.120)可能已修复或行为不同。
  2. 跳触发时机window.location.href的赋值是“导航”行为,ArkWeb可能在新版本中要求此类跳转必须由明确的用户手势(如click事件)同步触发,而不能由异步逻辑(如setTimeout、Promise回调、事件监听器的异步处理)触发。这是现代浏览器(包括移动端WebView)常见的安全策略。

建议的解决方案:

方案一(首选):使用 <a> 标签模拟点击 这是目前最兼容ArkWeb及现代浏览器安全策略的方式。确保跳转动作在一个真实的用户点击事件回调中同步执行。

function launchMiniProgram(schemeUrl) {
  const a = document.createElement('a');
  a.href = schemeUrl;
  a.style.display = 'none';
  document.body.appendChild(a);
  a.click(); // 同步触发点击
  document.body.removeChild(a);
}

// 必须在按钮点击等用户手势事件的同步回调中调用
document.getElementById('myButton').addEventListener('click', function(e) {
  launchMiniProgram('your-scheme://path?params=xxx');
  // 不要在此处使用setTimeout包裹
});

方案二:使用 window.open window.open方法在某些场景下对scheme的支持可能比location.href更好,但同样受用户手势规则限制。

document.getElementById('myButton').addEventListener('click', function(e) {
  window.open('your-scheme://path?params=xxx', '_self');
});

方案三:检查并申请必要的权限 确保您的H5页面或宿主应用已声明并申请了跳转至目标小程序所需的相关权限。这更多涉及原生配置,但可能是拉起失败的前提条件。

总结: 问题根源在于HarmonyOS Next的ArkWeb引擎对通过JavaScript进行scheme导航引入了更严格的用户交互要求。请优先将跳转代码重构为在用户手势事件的同步回调中,通过创建并点击<a>标签的方式来触发。同时,关注线上设备与测试设备在ArkWeb版本上的差异。

回到顶部