我想制作一个Flutter应用程序,要求之一是在Android或iPhone设备上打开本机电子邮件客户端。我不想创建新电子邮件,只需打开电子邮件应用程序即可。如果可能的话,我希望能够使用平台通用代码打开电子邮件客户端,如果没有,我想知道 iOS 端需要什么。我不是在寻找发送电子邮件意图,因为我知道 Flutter 中有一个插件可以实现这一点。作为一名 Android 开发人员,我相信我知道如何从 Flutter 调用 Intent 来实现隐式 Intent(如果我必须这样做的话),但我对 iOS 不熟悉。
您需要两个插件:
android_intent
和url_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){
;
});
}
使用 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:${[email protected]}'');
});
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>
我为此使用
open_mail_app: ^0.4.5
,它运行得很好,花了 2 分钟将其添加到我的应用程序中:
// Android: Will open mail app or show native picker.
// iOS: Will open mail app if single mail app found.
var result = await OpenMailApp.openMailApp();
它显示了我在 Android 智能手机上安装的所有 3 个电子邮件应用程序,但由于某种原因,它还提供了打开我的 paypal 应用程序的选项,这可能会让我的应用程序的用户认为有点可疑。
open_mail_app的开发者添加了过滤器来过滤paypal,但似乎不起作用:
/// Default filter list includes PayPal, since it implements the mailto: intent-filter
/// on Android, but the intention of this plugin is to provide
/// a utility for finding and opening apps dedicated to sending/receiving email.
你可以使用url_luncher:
await launchUrl(Uri.parse("mailto:email?subject=subject&body=body"))
& 不要使用 launch,因为它已被弃用:
launch("mailto:<email address>?subject=<subject>&body=<body>");
launch("mailto:<email address>?subject=<subject>&body=<body>");
使用 url_launcher 包
理论上这行代码应该可以解决问题
String email = '[email protected]';
String title = 'The subject';
String message = '';
await launch(mailto:$email?subject=$title&body=$message);
但由于某种原因,主题和正文被忽略,要解决此问题,请查看此答案,它应该可以顺利处理所有必需的参数。
工作代码:
final Uri params = Uri(
scheme: 'mailto',
path: email,
query: 'subject=$title&body=$message',
);
var url = params.toString();
await launch(url);
您可以使用email_launcher
示例
Email email = Email(
to: ['[email protected],[email protected]'],
cc: ['[email protected]'],
bcc: ['[email protected]'],
subject: 'subject',
body: 'body'
);
await EmailLauncher.launch(email);
这个答案可能是解决方案如何在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!")));
}
}
您可以使用此软件包在设备中打开邮件应用程序,https://pub.dev/packages/open_mail_app
这是我对上述问题的解决方案,目前正在运行!
sendAnEmail(String email) async {
const title = "Need support!";
const message = "";
final Uri url = Uri(
scheme: 'mailto',
path: email,
query: 'subject=$title&body=$message',
);
await launchUrl(url);
}
使用 url_launcher 插件 url_launcher
Future<void> _openUrl(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
适用于手机
_openUrl(Uri.parse('tel:${+917600896744}'));
用于电子邮件
_openUrl(Uri.parse('mailto:[email protected]'));
对于浏览器
_openUrl(Uri.parse('https://google.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>
这个库完美地为我解决了这个问题:https://pub.dev/packages/flutter_email_sender。例子很好,api简单明了。
我正在寻找类似的解决方案,我找到了这个包Open_mail_app。您可以阅读 readme.md 以了解如何在您的应用程序中使用它。
[我会尽快更新这个答案]