flutter相关URL schemes

先看效果

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

url_launcher | Flutter Package

配置 安卓

flutter 项目目录下的

android\app\src\main\AndroidManifest.xml

flutter相关URL schemes_第1张图片

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

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


		
		
			
			
		
		
		
			
			
		
		
			
			
		
	    
			
			
		
		
			
			
		
		
			
			
		
	

配置ios

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

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


    LSApplicationQueriesSchemes
		
		
			
				sms
			
			
				tel
			
			
				weixin
			
			
				tg
			
			
				whatsapp
			
		

使用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.delayed(Duration(seconds: seconds ?? 1));
  }

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

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

  // 打开微信
  static Future 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 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 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 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 标签内新增

		
				
				
				
				
			

flutter相关URL schemes_第2张图片

 ios配置

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


			CFBundleURLTypes
		
		
			
				
					CFBundleTypeRole
				
				
					Editor
				
				
					CFBundleURLName
				
				
					Two You
				
				
					CFBundleURLSchemes
				
				
					
						konnect
					
				
			
		

你可能感兴趣的:(flutter经验,flutter)