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

时间:2018-06-28 22:04:39

标签: ios email dart flutter

我要制作Flutter应用,其中一项要求是在Android或iPhone设备上打开本地电子邮件客户端。我不希望创建新电子邮件,只需打开电子邮件应用即可。我希望能够在可能的情况下使用平台通用代码打开电子邮件客户端,否则,我想知道iOS端需要什么。我不是在寻找发送电子邮件的意图,因为我知道Flutter中有一个用于此目的的插件。作为一名Android开发人员,我相信我必须知道如何从Flutter调用该隐式意图的意图,但是我对iOS不熟悉。

6 个答案:

答案 0 :(得分:9)

url_launcher插件可以做到

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

在默认电子邮件应用中创建要发送到的电子邮件

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

答案 1 :(得分:9)

您需要两个插件: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){
    ;
  });
}

答案 2 :(得分:1)

您可以使用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);

答案 3 :(得分:0)

使用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}'');
});

答案 4 :(得分:0)

此答案可能是解决方法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!")));
    }
}

答案 5 :(得分:-1)

该库为我完美地解决了这个问题:https://pub.dev/packages/flutter_email_sender。该示例很好,并且api简单明了。

相关问题