Open Urls in Browser, In App, Make Phone Calls, Open Installed Apps in Flutter

By | November 24, 2019
Launch Urls in Flutter

Launch Urls in Flutter


This demo will show how can we

  • Launch urls in a browser outside the App
  • Launch urls in a browser inside the App
  • Launch Installed app (eg:youtube)
  • Make Phone Call.

Launch Urls in Flutter

Launch Urls in Flutter


Watch Video Tutorial


Add Dependencies

To make all this work, we need the url launcher plugin.

Open the pubspec.yaml file in your project and add this plugin at the end of Dependencies.

  dependencies:
    flutter: 
      sdk: flutter

    # The following adds the Cupertino Icons font to your application.
    # Use with the CupertinoIcons class for iOS style icons.
    ...
    url_launcher: ^5.2.5 

Then run ‘flutter packages get’ to download and integrate the library into your app.


Launch url in browser

Future<void> _launchInBrowser(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: false,
        forceWebView: false,
        headers: <String, String>{'header_key': 'header_value'},
      );
    } else {
      throw 'Could not launch $url';
    }
}

In the above code, first we are going to check if the Url is Launchable, if yes then we call launch to launch the url in a browser. Whether to load url inside or outside the app is determined by the forceSafariVC property.
if you set forceSafariVC to true, then it will be launched inside the app otherwise it will be loaded in a browser outside the app.


Launch Universal links

The best example here is the youtube App. When you try to launch a youtube url, the app will launch it for you, correct.
We are going to achieve similar functionality here.

The below code helps you to achieve this.


  Future<void> _launchUniversalLinkIos(String url) async {
    if (await canLaunch(url)) {
      final bool nativeAppLaunchSucceeded = await launch(
        url,
        forceSafariVC: false,
        universalLinksOnly: true,
      );
      if (!nativeAppLaunchSucceeded) {
        await launch(url, forceSafariVC: true);
      }
    }
  }

Make Phone Call

The phone dialer is another app which you can open with the help of this library.

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

  // Call the above function in the proper phone number format
  // To Launch the phone dialer, you need to append tel: before the phone number.
   _makePhoneCall('tel:1234567890');

To close the launched webview inside the app, you can simply call closeWebView().

To see all the above code in action, please watch the Youtube tutorial.

Please leave your valuable comments below.


2 thoughts on “Open Urls in Browser, In App, Make Phone Calls, Open Installed Apps in Flutter

  1. Ultimate

    Doesn’t work properly on Andorid. If the application is installed I want to open it in the application. I want to view webview if it is not installed. please help me

    Reply
    1. James Post author

      It will open if the application is installed. Make sure your url is correct.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *