How to Open Default Email on a iPhone With Flutter ?

· 2 min read
How to Open Default Email on a iPhone With Flutter
How to Open Default Email on a iPhone With Flutter

When designing or developing a Mobile Application Sometimes users need to open the default email app from iPhone So in this article, we will learn about How to open the default email on an iPhone with Flutter?

How to Open Default Email on an iPhone With Flutter?

To Open a default email on a user can use a plugin android_intent like the below:

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){
    ;
  });
}

User can make a plugin like below:

Email email = Email(
    to: ['[email protected],[email protected]'],
    cc: ['[email protected]'],
    bcc: ['[email protected]'],
    subject: 'subject',
    body: 'body'
);
await EmailLauncher.launch(email);

Users can also try the below way.

Future<void> _launched;

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

Then for a phone consider a code snippet like below:

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

for email:

setState(() {
  _launched = _openUrl('mailto:${[email protected]}'');
});

User can make a custom function like OpenEmailApp below:

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!")));
    }
}

Conclusion:

Hope you find this article useful.

Drop us your Suggestion/Feedback for the same.

Still, need support for Flutter Development? Do let us know.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply