HarmonyOS鸿蒙Next中url_launcher的使用

HarmonyOS鸿蒙Next中url_launcher的使用 通过外链方式展示《隐私政策》时,报以下错误:

06-20 16:41:58.272 39710-39782 A00000/XComFlutterEngine com.xclou...xiaojian E Thread:34195732896 [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method launch on channel plugins.flutter.io/url_launcher)
06-20 16:41:58.272 39710-39782 A00000/XComFlutterEngine com.xclou...xiaojian E #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:313:7)
06-20 16:41:58.272 39710-39782 A00000/XComFlutterEngine com.xclou...xiaojian E <asynchronous suspension>
06-20 16:41:58.272 39710-39782 A00000/XComFlutterEngine com.xclou...xiaojian E #1 MineAboutPageState._body.<anonymous closure> (package:hmxiaojian/ui/page/mine/mine_about.dart:68:20)
06-20 16:41:58.272 39710-39782 A00000/XComFlutterEngine com.xclou...xiaojian E <asynchronous suspension>

出错代码:

--> if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
.....
}
-----------------------------

请问如何使用外链,支持哪个版本?


更多关于HarmonyOS鸿蒙Next中url_launcher的使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

url_launcher鸿蒙已适配,请使用适配版本

参考文档:https://gitee.com/openharmony-sig/flutter_packages/tree/master

更多关于HarmonyOS鸿蒙Next中url_launcher的使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)Next中,url_launcher 是一个用于启动外部URL的工具。它允许应用程序在设备上打开指定的网页链接,通常是通过系统的默认浏览器来实现的。url_launcher 的使用场景包括但不限于打开网页、启动邮件客户端、拨打电话等。

在HarmonyOS Next中,url_launcher 的使用流程如下:

  1. 导入依赖:首先需要在项目的 build.gradle 文件中添加 url_launcher 的依赖项。例如:

    dependencies {
        implementation 'ohos:url_launcher:1.0.0'
    }
    
  2. 创建URL链接:在你的代码中创建一个包含目标URL的字符串。例如:

    const url = 'https://www.example.com';
    
  3. 调用url_launcher:使用 url_launcher 提供的API来启动URL。例如:

    import { launchUrl } from 'url_launcher';
    
    launchUrl(url).then((success) => {
        if (success) {
            console.log('URL launched successfully');
        } else {
            console.log('Failed to launch URL');
        }
    });
    
  4. 处理回调launchUrl 方法返回一个 Promise,可以处理成功或失败的回调。成功时,URL将被系统默认的浏览器打开;失败时,可能是由于设备上没有合适的应用来处理该URL。

  5. 权限配置:如果需要在应用中打开外部URL,确保在 config.json 文件中配置了相应的权限。例如:

    {
        "module": {
            "reqPermissions": [
                {
                    "name": "ohos.permission.INTERNET"
                }
            ]
        }
    }
    

通过以上步骤,你可以在HarmonyOS Next应用中使用 url_launcher 来启动外部URL。url_launcher 的使用简单且高效,适合需要在应用中集成网页浏览功能的场景。

在HarmonyOS鸿蒙Next中,url_launcher用于打开外部URL链接。首先,在build.gradle中引入依赖:

dependencies {
    implementation 'ohos:url_launcher:1.0.0'
}

然后在代码中使用UrlLauncher类打开URL:

import ohos.app.Context;
import ohos.utils.net.Uri;
import ohos.utils.net.url.UrlLauncher;

public void openUrl(Context context, String url) {
    Uri uri = Uri.parse(url);
    UrlLauncher.launchUrl(context, uri);
}

调用openUrl方法即可在默认浏览器中打开指定URL。

回到顶部