Url launcher using flutter
Exploring the URL Launcher Package in Flutter: A Comprehensive Guide
Introduction:
In the world of Flutter app development, integrating external functionalities like opening URLs, making phone calls, sending emails, and more is essential for creating robust and user-friendly applications. This is where the URL Launcher package comes into play. This package provides a simple and efficient way to interact with external resources and services directly from your Flutter app.
What is a URL Launcher?
The URL Launcher package is a Flutter plugin that allows developers to launch various types of URLs and perform actions such as opening web pages, making phone calls, sending emails, and launching map applications. It abstracts the platform-specific code required for these tasks, making it easy to incorporate these functionalities into your Flutter apps regardless of the platform (Android or iOS).
Installation:
To start using the URL Launcher package in your Flutter project, follow these simple steps:
- Add the URL Launcher package dependency under the dependencies section:
→ flutter pub add url_launcher.
- Now add the following permissions in the manifest and info.plist file.
- Manifest file
→ <intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="sms" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tel" />
</intent>
- Info.plist file
→ <key>LSApplicationQueriesSchemes</key>
<array>
<string>sms</string>
<string>tel</string>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>mailto</string>
</array>
- Using the following functions you can use the url launcher.
- For call
→ void makePhoneCall() async {
const phoneNumber = 'tel:1234567890';
if (await canLaunch(phoneNumber)) {
await launch(phoneNumber);
} else {
throw 'Could not launch $phoneNumber';
}
}
- For sms
→ void sendSMS() async {
const smsNumber = 'sms:1234567890';
if (await canLaunch(smsNumber)) {
await launch(smsNumber);
} else {
throw 'Could not launch $smsNumber';
}
}
- For email
→ void sendEmail() async {
final Uri emailLaunchUri = Uri(
scheme: 'mailto',
path: 'recipient@example.com', // Enter your recipient email address here
queryParameters: {
'subject': 'Hello', // Enter your email subject here
'body': 'Hello Flutter!' // Enter your email body here
},
);
final url = emailLaunchUri.toString();
if (await canLaunchUrl(emailLaunchUri)) {
await launchUrl(emailLaunchUri);
} else {
throw 'Could not launch $url';
}
}
For detail code please visit the github code
Happy Coding!! 🙂