Share Things On Social Media Via An Ionic 2 Mobile App

Recently one of my subscribers asked me if I could refresh the social media sharing article that I wrote for Ionic Framework 1 to work with Ionic 2.  Since I’m already in the process of rewriting all my posts to Ionic 2, I figured now is a good time to do so.

In case you hadn’t seen my previous post, it is still worth checking out.  In either scenario, the goal we’re going to accomplish here is sharing messages, images, and links via social media outlets on Android and iOS within an Ionic 2 mobile app that uses Angular 2.

To be clear before we get too deep, the user needs to have the social media application installed on their device before it can be used.  This is only in the scenario where you want specifics.  There is a generic approach and we’re going to see both.

Let’s start by creating a new Ionic 2 project using the Command Prompt (Windows) or Terminal (Mac and Linux):

Two important things to note here.  First, you need to be using a Mac if you wish to add and build for the iOS platform.  Second, you need to be using the Ionic CLI that supports version two of the framework.

With the project set up, we now need to install the Apache Cordova plugin that will make this all possible.  Using the wonderful SocialSharing-PhoneGap plugin by Eddy Verbruggen we can get sharing features into our app.  This can be installed by executing the following:

Now we can start using it per the documentation.  The plugin is powered by vanilla JavaScript, but not a problem because Angular 2 works fine with vanilla JavaScript.

For this sample project we’re going to spend all our time in the app/pages/home/home.js and app/pages/home/home.html files.  These are our logic and UI files.  Starting with the logic file, open app/pages/home/home.js and include the following code:

Let’s break this down a bit.

Per the official plugin documentation we have the opportunity to use a few diverse functions.  We can share to whatever the device will let us, share to a particular network, and also check to see if we even can share to a particular network.  There is too much to the plugin to see everything so we’re just going to focus on three things.

In the above function we are preparing for a very generic social share.  This means the user can decide where they want to share based on the parameters passed.  Since the plugin uses native code it needs to be wrapped in the platform.ready otherwise you could face errors.  When the device is ready we check to see if the plugin is installed and exists and if it does, show the share menu.

The above function is a little different.  This time we are specifying where to share, being Twitter.  Not everyone will have Twitter installed on their device so we must first can to make sure we can share to Twitter using the canShareVia function.  If successful, then go ahead and share.

Now let’s look at our very simple UI.  Open the project’s app/pages/home/home.html file and include the following code:

Nothing fancy here right?  We just made two buttons to call each of the two functions that we built in the logic file.

Conclusion

You may have already seen it in my Ionic Framework 1 tutorial, but social media sharing needed a refresher for Ionic 2 which uses Angular 2 instead of AngularJS.

With the social media sharing plugin you can share text, images, and links to media networks like Twitter, Facebook, and Google+.  You can even share to email and SMS text message if you wanted to.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

  • usman rehman

    HeyNic,

    thanks for this article. can you please help me integrating fb login with ionic 2 and angular JS 2?

    Usman

  • arsene

    possible to implement it with the actionSheet?

  • nonren

    Great tutorial thanks. Just a quick questions, is there anyway to direct share to twitter without showing any dialog box?

    • You’ll have to use the Twitter SDK for Apache Cordova for that.

  • Adrián Brito Pacheco

    Another option: Social Sharing from IONIC Native.

    import { SocialSharing } from ‘ionic-native’;

    whatsappShare(message, image, url) {
    this.platform.ready().then(() => {
    SocialSharing.shareViaWhatsApp(message, image, url)
    .then(() => {
    alert(“Success”);
    }).catch(() => {
    // Error!
    alert(“Failed”);
    });
    });
    }

    https://ionicframework.com/docs/v2/native/social-sharing/