打开网页、拨号、发邮件、跳转自定义 scheme,是几乎所有应用都会用到的基础能力。url_launcher 在 Android 上发 Intent、在 iOS 上用 UIApplication.open,在鸿蒙上对应的是隐式 Want。

本文以 url_launcher 6.3.3 为例,记录我如何保留原有 Dart API、补齐 Flutter OH 插件入口、在鸿蒙端接入真实系统能力,并在 HarmonyOS API 26 真机上完成构建、安装、启动和按钮调用验证。

适配仓库: url_launcher

适配分支: feat/ohos_url_launcher_6.3.3

适配 TAG: 6.3.3-ohos.1

一、先看最终效果

图 1:HarmonyOS API 26 真机启动 Demo。页面显示 url_launcher 6.3.3。

图 2:点击“查询能否打开 https”后的真实返回:canLaunchUrl(https) → true。

图 3:点击“查询能否拨号”后的真实返回:canLaunchUrl(tel) → true。

图 4:点击“打开浏览器”后的真实返回:``。

验证点实测结果
Flutter OH 3.47 工程构建通过,产出 entry-default-signed.hap
安装、启动(真机)通过
查询能否打开 httpscanLaunchUrl(https) → true
查询能否拨号canLaunchUrl(tel) → true
打开浏览器``

二、这个库原来是怎么工作的

平台接口层只有一条 MethodChannel:

Dart UrlLauncherPlatform
  -> MethodChannel('plugins.flutter.io/url_launcher')
  -> Android Intent / iOS UIApplication.open

方法只有三个:canLaunch、launch、closeWebView。

三、实际环境

Flutter 鸿蒙环境搭建不在本文重复展开,直接参考 Flutter OH 环境搭建指南。

组件版本
Flutter OH3.47.4-ohos-1.0.4
Dart3.13.3
DevEco Studio26.0.0 Release
HarmonyOS SDKAPI 26
测试设备华为畅享 90 Pro Max,HarmonyOS 7.0.0.105(API 26)
目标库url_launcher 6.3.3
flutter --version
hdc list targets

四、确认上游版本并同步到 AtomGit

上游包地址是 https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher,版本 6.3.3 使用 BSD-3-Clause License。适配前我用 AtomGit API(GET /api/v5/repos/<org>/<lib>)在 oh-flutter、hxa-flutter、CPF-Flutter 三个组织做了排重,未发现同名 OHOS 实现。

仓库适配提交为 a75c9e8,分支与 TAG 6.3.3-ohos.1 均指向该提交。

git clone https://atomgit.com/oh-flutter/url_launcher.git
cd url_launcher
git switch feat/ohos_url_launcher_6.3.3
git log --oneline -5

五、创建适配分支并检查工程结构

git switch -c feat/ohos_url_launcher_6.3.3

关键目录如下:

url_launcher/
├── lib/                                         # 原有 Dart API 和通道
├── ohos/
│   ├── index.ets                                # OHOS 插件导出入口
│   └── src/main/ets/components/plugin/
│       └── UrlLauncherPlugin.ets    # 通道实现
├── example/lib/main.dart                        # 真机验证 Demo
├── example/ohos/                                # Flutter OH 宿主工程
├── pubspec.yaml                                 # 声明 ohos 平台
└── README.OpenHarmony_CN.md                     # 鸿蒙适配说明
flutter:
  plugin:
    platforms:
      ohos:
        pluginClass: UrlLauncherPlugin

pluginClass 必须和 ohos/index.ets 导出的类名一致。这一步在所有库里都踩过:名字对不上时 Dart 能编译,运行时却抛 MissingPluginException,没有任何构建期提示。

六、适配过程:注册通道并对齐语义

通道名必须与上游 Dart 源码逐字一致。本库的映射如下:

方法OpenHarmony 实现说明
canLaunchbundleManager.canOpenLink(url) + scheme 校验真实判断系统是否有 Ability 能处理该 URI
launchstartAbility({action:'ohos.want.action.viewData', uri})交给系统浏览器/拨号等应用
closeWebView空操作鸿蒙拉起的是系统应用,应用不持有 WebView

核心代码:

private async launch(url: string): Promise<boolean> {
  if (this.ability == null) {
    throw new Error('A foreground UIAbility is required.');
  }
  await this.ability.context.startAbility({
    action: 'ohos.want.action.viewData',
    uri: url,
  });
  return true;
}

七、构建配置与签名

示例工程的目标 SDK 对齐到 26,与 @ohos/flutter_ohos 的要求一致:

{
  "name": "default",
  "compatibleSdkVersion": "26.0.0",
  "targetSdkVersion": "26.0.0",
  "runtimeOS": "HarmonyOS"
}

Demo 的 AppScope/app.json5 统一成 com.example.flutter_oh_demo。签名 profile 里绑定的 bundleName 必须与它一致,否则 SignHap 直接报 00303074 Configuration Error。证书、p12、p7b 和密码不提交到仓库。

八、通过 AtomGit 引入适配版

在业务工程中使用时锁定 TAG:

dependencies:
  url_launcher:
    git:
      url: https://atomgit.com/oh-flutter/url_launcher.git
      ref: 6.3.3-ohos.1
flutter pub get

然后在 pubspec.lock 里确认 source: git、AtomGit 地址和 resolved-ref,避免解析到 pub.dev 的同名版本。

九、构建、安装和真机验证

cd example
flutter pub get
flutter build hap --debug
hdc install -r build/ohos/hap/entry-default-signed.hap
hdc shell aa start -a EntryAbility -b com.example.flutter_oh_demo

构建输出为 ✓ Built build/ohos/hap/entry-default-signed.hap.,随后 hdc 报告安装成功。所有示例共用同一个 bundleName,重装后要先 aa force-stop 再 aa start,否则系统只是把旧任务拉到前台,看到的是上一次安装的界面。

十、适配结果和当前边界

url_launcher 在鸿蒙上走隐式 Want,能否打开完全取决于系统里有没有注册对应 scheme 的应用。这一点和 Android/iOS 一致,不是适配缺陷。

十一、FAQ

Q1:为什么 about:blank 打不开?

  • 原因: about: 不是鸿蒙上任何应用会注册的 scheme,startAbility 返回 16000019 No matching ability is found。
  • 验证结果: 示例改用 https 与 tel 两个真实 scheme,canLaunchUrl 都返回 true。

Q2:为什么 launchUrl 后页面状态不变?

  • 原因: 拉起系统浏览器会把应用切到后台,页面不再刷新属预期。
  • 验证结果: 截图里可以看到浏览器被打开。

十二、总结

url_launcher 6.3.3 在鸿蒙上已经接入真实系统能力,Dart 侧 API 与上游保持一致。本文的验证结果全部来自 HarmonyOS API 26 真机按钮点击,没有占位返回值;平台本身不具备的能力在正文和 README 中都已明确标注为边界。

十三、参考资料和仓库地址

欢迎加入CPF-Flutter 鸿蒙社区:https://atomgit.com/CPF-Flutter

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐