Hello everyone. With this article, I would like to announce that I have started the ‘Flutter Package of the Week’ series, which I think will be useful when developing Flutter projects. The first week’s package is url_launcher, which I recently used in my own project.
What is url_launcher ?
If you want to redirect your users from your app to an external URL, a web page, an email, or even a phone number, this is the package you need.
- For example, when a button is pressed, the user is redirected to the default browser so that they can open a specific URL.
- Enabling a phone number to be copied into the phone’s standard dialling application and displayed.
- Prepare and send an email to generate the recipient and text when a button is pressed.
Installation
You can start the installation by typing the following line in the terminal:
flutter pub add url_launcher
It will be added to the pubspec.yaml file as follows:
dependencies:
url_launcher: ^6.1.7 //Version number may vary
Usage
1- Opening Web Urls
You can use the launch
function to open a web URL:
import 'package:url_launcher/url_launcher.dart';
void launchURL() async {
const url = 'https://flutter.dev';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Note: While canLaunch(url)
checks whether the given URL can be opened, launch(url)
opens the URL in the browser. You can also write the url directly in the code without writing the launch(url)
method, but there may be problems in the application in case of a possible inability to open the url related to the user’s device. For this reason, it is especially recommended to use the canLaunch(url)
function in the method.
2- Making a Phone Call
void makePhoneCall() async {
const phoneNumber = 'tel:+1234567890';
if (await canLaunch(phoneNumber)) {
await launch(phoneNumber);
} else {
throw 'Could not launch $phoneNumber';
}
}
Note: A telephone call is made by specifying a telephone number with the prefix tel:
.
3- Sending SMS
void sendSMS() async {
const sms = 'sms:+1234567890';
if (await canLaunch(sms)) {
await launch(sms);
} else {
throw 'Could not launch $sms';
}
}
Note: SMS to a specific number can be initiated using the sms:
prefix.
4- Sending E-mail
void sendEmail() async {
final Uri emailLaunchUri = Uri(
scheme: 'mailto',
path: 'example@example.com',
query: 'subject=Example Subject&body=This is a test email',
);
if (await canLaunch(emailLaunchUri.toString())) {
await launch(emailLaunchUri.toString());
} else {
throw 'Could not launch $emailLaunchUri';
}
}
Note: An e-mail is sent using the mailto:
scheme. The subject and body content can be specified in the query
section.
Application Permissions
iOS
You must allow internet access by adding the following line to the ios/Runner/Info.plist
file:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Android
You must provide internet access permission in android/app/src/main/AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET"/>
I hope you found this article useful! If you appreciate the information provided, you have the option to support me by Buying Me A Coffee! Your gesture would be greatly appreciated!
I hope I have been able to present the package in detail, I wish it to be useful. See you in the coming weeks with other packages.
Have a good day.
Selin.
Hiç yorum yok:
Yorum Gönder