Abhay
Abhay

Reputation: 347

Open social media apps from flutter app using url_launcher

I want to redirect from my flutter app to social media platform apps using a profile/business URL. I am using the url_launcher dart package. I was able to find the endpoint to redirect to a Facebook profile but don't know how to open specific Instagram and Twitter pages.

_launchFacebook(String? facebook) async {
    final url = 'fb://facewebmodal/f?href=https://' + facebook!;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _launchTwitter(String? twitter) async {
    final url = 'tw://' + twitter!;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _launchInstagram(String? instagram) async {
    final url = 'in://' + instagram!;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }

Upvotes: 5

Views: 9105

Answers (5)

Nikita
Nikita

Reputation: 1

I implementing this code in my project and it is working fine.

onTap: () async {
                   

    if (shareList[index]['title'] == "Whatsapp") {
                  

        final url = "https://wa.me/?text=${articleLink.toString()}";
                  

        if (await canLaunchUrl(Uri.parse(url))) {
    

            await launchUrl(Uri.parse(url));
                Navigator.pop(context);
              } else {
                throw 'Could not launch $url';
              }
            } else if (shareList[index]['title'] == "Facebook") {
              final facebookPageUrl = Uri.parse(
                  "fb://facewebmodal/f?href=https://www.facebook.com/$articleLink");
              if (await canLaunchUrl(facebookPageUrl)) {
                await launchUrl(facebookPageUrl,
                    mode: LaunchMode.externalApplication);
                Navigator.pop(context);
              } else {
                final fallbackUrl = Uri.parse(
                    "https://www.facebook.com/$articleLink");
                Navigator.pop(context);
                if (await canLaunchUrl(fallbackUrl)) {
                  await launchUrl(fallbackUrl,
                      mode: LaunchMode.externalApplication);
                } else {
                  throw 'Could not launch Facebook URL';
                }
              }
            } else if (shareList[index]['title'] == "Gmail") {
              final Uri emailUri = Uri(
                scheme: 'mailto',
                path: '',
                query: Uri.encodeFull(
                    'subject=Send a Article Link&body=$articleLink'),
              );
              if (await canLaunchUrl(emailUri)) {
                await launchUrl(emailUri);
                Navigator.pop(context);
              } else {
                throw 'Could not launch email app';
              }
            } else if (shareList[index]['title'] == "LinkedIn") {
              final profileUrl = Uri.parse(
                  "https://www.linkedin.com/sharing/share-offsite/?url=${Uri.encodeComponent(articleLink)}");
              if (await canLaunchUrl(profileUrl)) {
                await launchUrl(profileUrl,
                    mode: LaunchMode.externalApplication);
                Navigator.pop(context);
              } else {
                throw 'Could not launch LinkedIn profile';
              }
            } else {
              Clipboard.setData(ClipboardData(text: articleLink))
                  .then((_) {
                ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: Text("Link copied to clipboard")));
              });
              // Clipboard.setData(ClipboardData(text: articleLink));
              Navigator.pop(context);
            }
          },

Upvotes: 0

Hesty
Hesty

Reputation: 11

You can without any problem open apps directly with web link.

Instagram, twitter, youtube, linkdln and tiktok work perfectly with web link like https://instagram.com/'username' etc.

Specifically, you can use this link for Facebook : https://facebook.com/profile.php?id=your_id

void _launchURL(String url) async {
    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
    } else {
      throw 'Could not launch $url';
    }
  }

Upvotes: 1

PK Chahar
PK Chahar

Reputation: 111

It's worked for me

 launchUrlString('https://www.linkedin.com/');

Upvotes: -1

Osama Remlawi
Osama Remlawi

Reputation: 2990

A simple way to do it:

Future<void> _launchSocialMediaAppIfInstalled({
  String url,
}) async {
  try {
    bool launched = await launch(url, forceSafariVC: false); // Launch the app if installed!

    if (!launched) {
      launch(url); // Launch web view if app is not installed!
    }
  } catch (e) {
    launch(url); // Launch web view if app is not installed!
  }
}

And then simply call it like this:

_launchSocialMediaAppIfInstalled(
  url: 'https://www.instagram.com/avey.world/', //Instagram
);

_launchSocialMediaAppIfInstalled(
  url: 'https://www.facebook.com/avey.pal/', // Facebook
);

_launchSocialMediaAppIfInstalled(
  url: 'https://twitter.com/avey_pal', // Twitter
);

_launchSocialMediaAppIfInstalled(
  url: 'https://www.linkedin.com/company/avey-ai/', // Linkedin
);

...

Don't forget to replace the example page by yours ;) and that's it!

Upvotes: 6

Me&#239; M.
Me&#239; M.

Reputation: 321

Facebook is a bit of an odd case here, asking for a specific protocol fb://facewebmodal/f?href=$url.

Usually, applications register the URL of their website for deep linking, which means they will open in your browser if you don't have the corresponding app installed, and directly in their app if you do.

This is the implementation I usually use, which definitely works for Facebook, Twitter, Instagram and LinkedIn. I included a special check for Android, so that if you don't have Facebook installed, it will open up the link correctly in the browser instead.

  @override
  Future<void> launchUrl(String url) async {
    final _canLaunch = await canLaunch(url);
    if (kIsWeb) {
      if (_canLaunch) {
        await launch(url);
      } else {
        throw "Could not launch $url";
      }
      return;
    }
    if (TargetPlatform.android) {
      if (url.startsWith("https://www.facebook.com/")) {
        final url2 = "fb://facewebmodal/f?href=$url";
        final intent2 = AndroidIntent(action: "action_view", data: url2);
        final canWork = await intent2.canResolveActivity();
        if (canWork) return intent2.launch();
      }
      final intent = AndroidIntent(action: "action_view", data: url);
      return intent.launch();
    } else {
      if (_canLaunch) {
        await launch(url, forceSafariVC: false);
      } else {
        throw "Could not launch $url";
      }
    }
  }

You can without any problem open links in the https://instagram.com/username and the https://twitter.com/username formats with it.

Hope it helps !

Upvotes: 8

Related Questions