Flutter教程url_launcher打开第三方软件

在Flutter中使用url_launcher插件打开第三方软件时遇到了问题。按照官方文档配置了AndroidManifest.xml并添加了<intent-filter>,但点击链接后系统提示“未找到应用程序”。测试的URL格式包括https://、tel:和mailto:,只有浏览器和拨号应用能正常触发,其他应用如淘宝、微信都无法跳转。请问:

  1. 是否需要为不同应用配置特定的URL Scheme?哪里能查到主流应用(如淘宝、微信)的正确Scheme?
  2. 如果目标应用未安装,如何捕获异常并引导用户到应用商店?
  3. iOS是否需要额外配置?看到有资料说还需要在Info.plist中添加LSApplicationQueriesSchemes,但不确定具体步骤。
    求有实际经验的开发者分享解决方案!

更多关于Flutter教程url_launcher打开第三方软件的实战教程也可以访问 https://www.itying.com/category-92-b0.html

3 回复

在Flutter中使用url_launcher插件可以方便地打开第三方应用。首先确保在pubspec.yaml文件中添加依赖:

dependencies:
  url_launcher: ^6.0.9

然后执行flutter pub get

示例代码如下:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Url Launcher Demo')),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              const url = 'tel://1234567890'; // 可替换为其他scheme如mailto:, http://
              if (await canLaunch(url)) {
                await launch(url);
              } else {
                throw '无法打开 $url';
              }
            },
            child: Text('拨打电话'),
          ),
        ),
      ),
    );
  }
}

注意:对于Android需在AndroidManifest.xml声明权限(如拨打权限),iOS需在Info.plist配置白名单。

更多关于Flutter教程url_launcher打开第三方软件的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


使用url_launcher插件可以轻松调起第三方应用。首先确保已添加依赖:

dependencies:
  url_launcher: ^6.0.9

然后在代码中这样使用:

import 'package:url_launcher/url_launcher.dart';

Future<void> _launchApp() async {
  const url = 'whatsapp://send?phone=1234567890'; // 示例:WhatsApp

  if (await canLaunch(url)) {
    await launch(url);
  } else {
    print('无法打开链接');
  }
}

注意:某些协议(如https)直接可用,但像whatsapp需要目标设备安装对应APP。如果想打开浏览器回退方案,可设置forceSafariVCuniversalLinksOnly等参数。

完整文档参考:官方文档

在Flutter中,使用url_launcher插件可以轻松打开第三方应用(如浏览器、地图、电话等)。以下是完整使用教程:

  1. 首先添加依赖到pubspec.yaml
dependencies:
  url_launcher: ^6.1.11
  1. 基本使用方法:
import 'package:url_launcher/url_launcher.dart';

// 打开网页
void _launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

// 拨打电话
void _makePhoneCall() async {
  const url = 'tel:+123456789';
  if (await canLaunch(url)) {
    await launch(url);
  }
}

// 发送邮件
void _sendEmail() async {
  const url = 'mailto:test@example.com?subject=Hello&body=Hi';
  if (await canLaunch(url)) {
    await launch(url);
  }
}

// 打开地图
void _openMap() async {
  const url = 'https://maps.google.com/?q=37.422,-122.084';
  if (await canLaunch(url)) {
    await launch(url);
  }
}
  1. 安卓配置(AndroidManifest.xml):
<queries>
  <!-- 如果要打开特定应用 -->
  <package android:name="com.twitter.android" />
  
  <!-- 通用intent处理 -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
</queries>
  1. iOS配置(Info.plist):
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>https</string>
  <string>tel</string>
  <string>mailto</string>
</array>

注意事项:

  • 调用前务必用canLaunch()检查可用性
  • 不同URL scheme对应不同应用(http/https、tel、mailto等)
  • 安卓11+需要添加<queries>配置
  • iOS需要配置白名单

这个插件支持大多数常用URL scheme,可以打开手机上的各种默认应用。

回到顶部