Implement Social Media Sharing In A NativeScript App

Every app lately has some social media aspect to it. Most commonly, apps will allow you to share some kind of data, whether it be an image or text, to social media networks such as Facebook or Twitter. Sharing via email or SMS message still is classified as social sharing. When building NativeScript Android and iOS applications, sharing data isn’t complicated.

We’re going to see how to share image data and text data on social media networks via a Telerik NativeScript Android and iOS mobile application.

Let’s start by creating a fresh NativeScript project. This can be done by executing the following from the Command Prompt (Windows) or Terminal (Mac and Linux):

At the top you’ll notice we are importing the plugin module and the module that will let us load image data. Then we create both a shareText and a shareImage function.

Per the plugin documentation, the sharing module’s shareText function expects a message and an optional subject. The module’s shareImage function expects an image and an optional subject. We’re loading the image data from a remote URL, but the image-source module has many other methods for loading data as seen in the official documentation.

Now let’s look at the app/main-page.xml file. Open it and include the following code:

We only have two buttons here. One will allow us to share text when clicked and the other will allow us to share the remote image when clicked. Nothing fancy at all here.

When you build your project you’ll be able to do social sharing on iOS and Android. Something to note though is that both platforms have different sharing options and more options become available when you have more apps installed.

Conclusion

You just saw how to share text and images through various social outlets. Yes I admit that I took most of this source code from the official documentation, but I made some revisions to make it easier to understand.

Different social media options will become available on each of the platforms as more apps get installed on the users device. The simulator by default will have very limited sharing options.

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.