URL Launcher Flutter
Overview
The URL launcher is a Flutter package that offers a straightforward way to call, text, send, and launch URLs from within a Flutter application. It offers a selection of features that make it simple to start an outside program or service that can deal with a certain URL scheme.
The features include opening a URL in the device’s default browser, dialing the provided number on the phone, Sending an email to a specific email address, and a text message to a specific phone number.
Introduction
URL Launcher Flutter is a flutter tool (url_launcher) to launch a URL. Supports web, phone, SMS, and email schemes. It is supported only for Android SDK version 16 and above, IOS version 11.0 and above, any Linux versions, macOS 10.11 and above, windows 10 and above, and any web version. To know more about the URL launcher flutter plugin visit here.
The URL Launcher Flutter package has made launching any URL or other flutter schemes incredibly simple. The URLs can only be launched by importing a package and using the URL launcher Flutter in a single line of code, which will parse the URL as a string. Other additional features in the package can also be used.
Prerequisites
- Android Studio or VS Code as an IDE for code execution and building the application.
- Virtual machines or use a mobile device to connect with developers option enabled.
- Basic Knowledge of Flutter and Dart as a programming language is needed for using the URL launcher Flutter feature.
- Flutter should be installed and add the created path in the Path section of environment variables of your system.
Installation and Configuration
- To use url launcher flutter plugin add url_launcher as a dependency in your pubspec. yaml file.
- To use the package we need to import the package using the import 'package:url_launcher/url_launcher.dart'; in every dart file that uses the URL launcher flutter feature.
- IOS configuration for using URL launcher flutter :
- If any URL schemes are passed to canLaunchUrl then LSApplicationQueriesSchemes must be added as an entry in your Info. list file, or else it will return false.
- Example :
- Check out this link for more information.
- Android configuration for using URL launcher flutter:
- Any URL schemes passed to canLaunchUrl must be passed as < queries > entries in your AndroidManifest.xml, or else it will return false in most cases starting on Android 11 (API 30) or higher. An < queries > element must be added to your manifest as a child of the root element.
- Example
For more details on this and other queries check out the android documentation
Supported URL schemes
The host platform receives the supplied URL and handles it there. The platform and installed programs determine the supported URL schemes.
Typical schemes consist of :
-
Checking supported schemes
-
Before using a scheme, the URL launcher flutter package uses canLaunchUrl() to determine at runtime if it is guaranteed to work.
-
Even while canLaunchUrl() would work in some circumstances, it can still return false. (in web apps, on mobile without the appropriate setup as explained above, etc.), thus it is preferable to use launchUrl() directly and manage failure in situations where you can offer fallback behavior. For instance, a UI button that would have sent feedback by email using a mailto URL might instead launch a web-based feedback form using an HTTPS URL on failure if canLaunchUrl returns false for mailto.
-
Encoding URLs
-
Particularly when we include spaces or other special characters, URLs must be correctly encoded. The Uri class often handles this automatically.
-
The query parameter and the encodeQueryParameters function should be used instead of Uri's queryParameters constructor argument for any query parameters for any scheme other than HTTP or HTTPS owing to a flaw in the way Uri encodes query parameters. Spaces will frequently be changed to plus signs (+) when queryParameters are used.
- Encoding for sms is slightly different by using URL launcher flutter :
-
URLs not handled by Uri
-
Rarely, you might need to launch a URL that the host system accepts as genuine but that Uri cannot represent. Importing URL launcher string. dart makes alternative APIs that use strings available in certain circumstances.
-
In URL launcher flutter it is highly advised against using these APIs in any other circumstances as giving erroneous URL strings was a major cause of issues with the plugin's original APIs.
-
File scheme handling
-
file: Scheme can be used on desktop platforms: Windows, macOS, and Linux.
-
Checking first whether the directory or file exists before calling launchUrl is recommended.
-
Example
Browser vs in-app Handling
Web URLs can be opened in the usual browser or via an in-app web view on various systems. Although a specific mode can be utilized on supported platforms by specifying a LaunchMode, the default behavior depends on the platform (check out launchUrl for information).
Project setup
- After everything is ready for Flutter, you may launch the following commands to build a new application:
Navigate to the new project directory:
Using flutter create will produce a demo application
- The URL launcher plugin must now be included in our pubspec.yaml file:
On your preferred device or the iOS or Android simulator, we can now begin running this.
- Now launch your code editor and open main.dart.
Replace every line in this file with a MaterialApp that directs the user to their home's HomePage.
Next, add the following lines of code to a fresh home.dart file:
URL Launcher async Functions
An async function is used when your application can accomplish tasks while awaiting the completion of another action thanks to asynchronous operations. Below are a few typical asynchronous operations :
- Obtaining information over a network.
- Data entry into a database.
- Data reading from a file.
Since URL launcher flutter obtains information over a network we need to wrap it up as an async function.
Example
We will be editing the on-tap button to implement different functionalities of the URL launcher flutter tool
- Launching a webpage with URL Launcher
Click on url launcher flutter gives :
- Launching maps
On clicking the url launcher flutter button we get
- Launching a telephone dialer
On clicking the url launcher flutter button we get
- Launching a text message
On clicking url launcher flutter :
- Launching an E-mail
On clicking the url launcher flutter button we get :
Conclusion
- The Flutter URL launcher plugin is a strong tool that enables developers to open external URLs from within their Flutter apps. The operating system is properly integrated, allowing the app to open online sites, email apps, phone dialers, map apps, and more.
- Throughout this article, we discussed the various use cases of the URL launcher plugin, including opening URLs in different apps, passing parameters, and handling different URL schemes. We also explored how to implement the plugin in Flutter, including installing the plugin, importing it into the app, and using it to launch URLs.
- The example we used for the URL launcher plugin in Flutter demonstrated how easy it is to integrate external URL functionality into a Flutter app. By following the steps outlined in the article, including installing the plugin, importing it into the app, and utilizing its methods, we were able to open external URLs such as web pages and email apps from within the Flutter app.
- In conclusion, Flutter's URL launcher plugin is a useful tool for creating feature-rich, powerful apps that can easily communicate with external services. Flutter developers may create dynamic, engaging apps that offer a cool user experience by sticking to best practices and utilizing the features of the URL launcher plugin.