8

How to launch Urls in Flutter

 3 years ago
source link: https://medium.com/codechai/how-to-launch-urls-in-flutter-66611ef141f8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Launching mail-, phone-, sms- or web-urls with the url-launcher in Flutter

Photo by SpaceX on Unsplash

Hello and welcome to my post about How to launch Urls in Flutter. Through regular content, applicable examples, and best practices, I’ll teach you how to use Flutter and Dart a little bit more every week. If you want to see the first post of this series head over to How to validate Emails in Flutter.

Url-launcher

Your app isn’t always everything. It happens that you provide an external url that links to a website. Or you provide an email address or even a phone number in the contact form, to reach the support. For all these external links, it would be nice to open them directly through the app using your browser, phone or email app. This is possible by implementing the url-launcher package in your flutter app.

The purpose

The Url-launcher package has the purpose to open external links. This includes website urls, as well as email, phone and sms links. They will then be executed by your smartphone (or other electronic device) in the corresponding app.

Package scope

The most important function provided by the package is the launch function. After you have installed the package and imported it into your class you can use the function launch(urlToLaunch) to execute a given url.
Thereby you have to consider the following:

  1. The launch function must be executed asynchronously. This can be achieved by wrapping a function around it and making it async.
  2. To prevent a PlatformException the function canLaunch(urlToLaunch) Before execution this function should always check if the url is valid.

You might now want to know what kind of schemes you can run and how by using this function? As already indicated, the package supports the following schemes to open in their default apps:

  • Open an Url: http:<URL> , https:<URL>, e.g. http://flutter.dev
  • Open a new email: mailto:<email address>?subject=<subject>&body=<body>, e.g. mailto:[email protected]?subject=News&body=New%20plugin
  • Make a phone call tel:<phone number> ,facetime:<phone number>(only on iOS), e.g. tel:+1 234 567 890
  • Send an SMS sms:<phone number>, e.g. sms:123456789

Consider
A given mobile device may not be able to receive all supported URL schemes. For example, a tablet may not have any mobile service and therefore may not support invoking a URL using the SMS scheme.

When calling a website url you have several different opinions:

  • Open in Android WebView (Android only): forceWebView = true
  • Enable JavaScript in WebView (Android only): enableJavaScript = true
  • Enable DOM storage in WebView (Android only): forceWebView = true
  • Add Extra headers to WebView (Android only): headers = {'header_key': 'header_value'}
  • Open in Safari View Controller (only on iOS >= 9.0): SafarieVC = true
  • Launch only universal links (only on iOS >= 10.0): universalLinksOnly = true
  • Set the status bar brightness after opening the link (iOS only): statusBarBrightness = Brightness.dark
  • Open in the current Tab (Web only): webOnlyWindowName = '_self'

An example

The following example shows a minimal application to open a web page url with the url-launcher package. The onPressed parameter of the RaisedButton calls the asynchronous function _launchUrl(). It then checks if it can launch the predefined url and acts accordingly. You can optionally give any of the above options along with the launch function.

The resulting UI looks like in the example video below:

Limitations

During my research, I could not find any restrictions in the use of the package. In my opinion, the package is very well developed and usable in all Flutter apps that requires it.

Installation

Installing the package you can use the flutter command in the command line or simply add the latest version of url_launcher in your pubspec.yaml under dependencies.

Run this command with Flutter:

$ flutter pub add url_launcher

This will add a line like this to your package’s pubspec.yaml (and run an implicit dart pub get):

dependencies:
url_launcher: ^6.0.9

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:url_launcher/url_launcher.dart';

Android

Starting from API 30 Android requires package visibility configuration in your AndroidManifest.xml otherwise canLaunch will return false. A <queries> element must be added to your manifest as a child of the root element.

The snippet below shows an example for an application that uses https, tel, and mailto URLs with url_launcher. See the Android documentation for examples of other queries.

Version

The latest version at the time of publishing the post is 6.0.1. The newest version supports null safety with Flutter 2. Looking at the repository, you can see that the development team behind this package is also developing other packages and keeping all of them up to date.

Scores

The popularity of the package is the best we have since now: 2914 Likes, 130 Pub Points from Flutter, and a Popularity of 100% and this is definitely justified.

Furthermore, the package supports null safety and all current platforms (Android, IOS, Linux, macOS, Web, Windows). It’s first published in April 2017 and supports null safety officially since February this year. It’s under the BDS License. The package provide several tests for all plattforms to ensure functionality and executability. It also has several packages depending on its logic.

Rating

Long before creating this blog post I came across the url_launcher and have already integrated it into several projects. But I had no idea how powerful this package really is and i am still amazed at how usable this package is. Therefore I gladly give this package a rating of 10 out of 10.

Links

You can find the latest package version at pub.dev underurl_launcher.

If you want to check out the app code with every package I wrote about, head over to lucianojung/flutter_package_examples.

If you are curious about the source code of the package visit flutter/plugins/url_launcher.

Head over to flutter-package-examples.netlify.app to see all the Flutter package examples and try them yourself.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK