先看效果

使用 url_launcher库 做唤起其他app操作

url_launcher | Flutter Package

配置 安卓

flutter 项目目录下的

android\app\src\main\AndroidManifest.xml

 如果不配置的话 有些手机就打不开app

微信有两个 一个是weixin 一个是wechat

<queries>
		<!-- If your app checks for SMS support -->
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="sms" />
		</intent>
		<!-- If your app checks for call support -->
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="tel" />
		</intent>
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="weixin" />
		</intent>
	    <intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="wechat" />
		</intent>
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="tg" />
		</intent>
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="whatsapp" />
		</intent>
	</queries>

配置ios

直接修改会出问题 在xcode 中去新增 配置

ios\Runner\Info.plist  <dict> 标签内部新增

<key>
    LSApplicationQueriesSchemes
		</key>
		<array>
			<string>
				sms
			</string>
			<string>
				tel
			</string>
			<string>
				weixin
			</string>
			<string>
				tg
			</string>
			<string>
				whatsapp
			</string>
		</array>

使用url_launcher库

import 'dart:io';
import 'package:app/common/util/k_log_util.dart';
import 'package:url_launcher/url_launcher.dart';

import 'k_toast_util.dart';

class KDefaultUtils {
  static Future sleep(int? seconds) {
    return Future<void>.delayed(Duration(seconds: seconds ?? 1));
  }

  /// 判断文件是否存在
  static Future<bool> fileExist(String path) async {
    if (path.isEmpty) return false;
    return await File(path).exists();
  }

  /// 开的app
  static Future<void> toLaunchUrl(url) async {
    final Uri _url = Uri.parse(url);
    if (!await launchUrl(_url)) {
      throw Exception('Could not launch $_url');
    }
  }

  // 打开微信
  static Future<bool> openWx() async {
    final Uri launch = Uri.parse('weixin://');
    bool isInstall = await canLaunchUrl(launch);
    if (isInstall) {
      await launchUrl(launch);
      return true;
    } else {
      KLogUtil.d("无法进行");
      KToastUtil.showToastMsg("请先安装app");
      return false;
    }
  }

  // 打开WhatsApp
  static Future<bool> openWhatsApp(String text) async {
    final Uri launch = Uri.parse('whatsapp://send?text=$text');
    bool isInstall = await canLaunchUrl(launch);
    if (isInstall) {
      await launchUrl(launch);
      return true;
    } else {
      KLogUtil.d("无法进行");
      KToastUtil.showToastMsg("请先安装app");
      return false;
    }
  }

  // 打开推特
  static Future<bool> openTelegram() async {
    final Uri launch = Uri.parse('tg://');
    bool isInstall = await canLaunchUrl(launch);
    if (isInstall) {
      await launchUrl(launch);
      return true;
    } else {
      KLogUtil.d("无法进行");
      KToastUtil.showToastMsg("请先安装app");
      return false;
    }
  }

  // 打开手机
  static openPhoneCall([String? phoneNumber]) async {
    final Uri launchUri = Uri(
      scheme: 'tel',
      path: phoneNumber ?? "",
    );
    await launchUrl(launchUri);
  }

  // 打开短信
  static openSmS(String body, [String? phoneNumber]) async {
    if (Platform.isAndroid) {
      final Uri launchUri = Uri.parse("sms:?body=${body}");
      await launchUrl(launchUri);
    } else {
      // ios
      final Uri launchUri = Uri.parse("sms:&body=${body}");
      await launchUrl(launchUri);
    }
  }

  // 打开本地应用商店
  static openItmsApp() async {
    final Uri launchUri = Uri.parse('itms-apps://');
    await launchUrl(launchUri);
  }

  // webview 打开链接
  static Future<void> launchInWebViewWithoutJavaScript(Uri url) async {
    if (!await launchUrl(
      url,
      mode: LaunchMode.inAppWebView,
      webViewConfiguration: const WebViewConfiguration(enableJavaScript: true),
    )) {
      throw Exception('Could not launch $url');
    }
  }
}

 // 打开短信 自动填充 联系人和内容

  static openSmS(String body, [String? phoneNumber]) async {

    final Uri launchUri =

        Uri(scheme: 'sms', path: phoneNumber ?? "", query: "body=$body");

    await launchUrl(launchUri);

  }

自定义自己app的Schemes

android\app\src\main\AndroidManifest.xml 下 activity 标签内新增

		<intent-filter>
				<action android:name="android.intent.action.VIEW" />
				<category android:name="android.intent.category.DEFAULT" />
				<category android:name="android.intent.category.BROWSABLE" />
				<data android:scheme="konnect" />
			</intent-filter>

 ios配置

ios\Runner\Info.plist 相同的位置

<key>
			CFBundleURLTypes
		</key>
		<array>
			<dict>
				<key>
					CFBundleTypeRole
				</key>
				<string>
					Editor
				</string>
				<key>
					CFBundleURLName
				</key>
				<string>
					Two You
				</string>
				<key>
					CFBundleURLSchemes
				</key>
				<array>
					<string>
						konnect
					</string>
				</array>
			</dict>
		</array>

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐