如何使用 Flutter 打开 iPhone 上的默认电子邮件应用程序?

Posted

技术标签:

【中文标题】如何使用 Flutter 打开 iPhone 上的默认电子邮件应用程序?【英文标题】:How do you open the default email app on an iPhone with Flutter? 【发布时间】:2018-12-08 01:28:31 【问题描述】:

我想制作一个 Flutter 应用,其中一个要求是在 android 或 iPhone 设备上打开本机电子邮件客户端。我不想创建新电子邮件,只需打开电子邮件应用程序。如果可能的话,我希望能够使用平台通用代码打开电子邮件客户端,否则我想知道 ios 端需要什么。我不是在寻找 Send Email Intent,因为我知道 Flutter 中有一个插件。作为一名 Android 开发人员,我相信我知道如何从 Flutter 调用 Intent 以实现该隐式 Intent,如果我必须这样做的话,但我对 iOS 并不熟悉。

【问题讨论】:

【参考方案1】:

使用 url_launcher 插件url_launcher

Future<void> _launched;

Future<void> _openUrl(String url) async 
    if (await canLaunch(url)) 
      await launch(url);
     else 
      throw 'Could not launch $url';
    

然后是电话

setState(() 
  _launched = _openUrl('tel:$+917600896744');
);

电子邮件

setState(() 
  _launched = _openUrl('mailto:$sejpalbhargav67@gmail.com'');
);

2021 年 7 月更新

在清单文件中添加以下行

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<queries>
<intent>
  <action android:name="android.intent.action.VIEW" />
  <data android:scheme="https" />
</intent>
<intent>
  <action android:name="android.intent.action.DIAL" />
  <data android:scheme="tel" />
</intent>
<intent>
  <action android:name="android.intent.action.SEND" />
  <data android:mimeType="*/*" />
</intent>

【讨论】:

这将发送一封电子邮件,而不是操作人员想要的。【参考方案2】:

这个答案可能是解决方案How to open default email app inbox in flutter?

void openEmailApp(BuildContext context)
    try
        AppAvailability.launchApp(Platform.isIOS ? "message://" : "com.google.android.gm").then((_) 
                print("App Email launched!");
              ).catchError((err) 
                Scaffold.of(context).showSnackBar(SnackBar(
                    content: Text("App Email not found!")
                ));
                print(err);
              );
     catch(e) 
      Scaffold.of(context).showSnackBar(SnackBar(content: Text("Email App not found!")));
    

【讨论】:

AppAvailability 是一个过时的插件,如果你使用它,它会在你发布构建时生成。所以请避免使用这个插件【参考方案3】:

这个库完美地为我解决了这个问题:https://pub.dev/packages/flutter_email_sender。例子不错,api简单明了。

【讨论】:

操作员明确表示他不想发送电子邮件。 该库允许您在不发送电子邮件的情况下打开电子邮件应用程序。【参考方案4】:

您可以使用email_launcher

例子

Email email = Email(
    to: ['one@gmail.com,two@gmail.com'],
    cc: ['foo@gmail.com'],
    bcc: ['bar@gmail.com'],
    subject: 'subject',
    body: 'body'
);
await EmailLauncher.launch(email);

【讨论】:

【参考方案5】:

您需要两个插件:android_intenturl_launcher

if (Platform.isAndroid) 
  AndroidIntent intent = AndroidIntent(
    action: 'android.intent.action.MAIN',
    category: 'android.intent.category.APP_EMAIL',
  );
  intent.launch().catchError((e) 
    ;
  );
 else if (Platform.isIOS) 
  launch("message://").catchError((e)
    ;
  );

【讨论】:

在 Android 上,这将在您的应用程序中打开电子邮件应用程序。要在新窗口中打开它,请添加:flags: [Flag.FLAG_ACTIVITY_NEW_TASK] launch("message://") 在模拟器中不起作用 android_intent 已替换为 android_intent_plus【参考方案6】:

url_launcher 插件可以做到这一点

mailto:<email address>?subject=<subject>&body=<body>

在默认电子邮件应用中创建电子邮件

另见How do I open a web browser (URL) from my Flutter code?

【讨论】:

我不想创建新的电子邮件,我只想打开默认的电子邮件应用程序。 抱歉,不知道这个 尝试message: 而不是mailto: ... 您可能需要使用 Uri.encodeFull('It is not ok')Uri.encodeComponent('It isnot ok') 并传递结果值。 @AlejandroIván 仅适用于 iOS 吗?

以上是关于如何使用 Flutter 打开 iPhone 上的默认电子邮件应用程序?的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Web - 如何在桌面上的 Chrome 中测试 Flutter Web 应用程序打开?

如何让我的应用程序显示在 iPhone 上的“打开方式...”列表中?

Flutter Run 不工作,在 iPhone 上启动应用程序时出错

转-如何使用iTunes制作iPhone铃声

在没有配套应用程序的情况下从 Apple Watch 打开 Iphone 上的浏览​​器

Flutter:如何在 webview 中阻止全屏视频?