Use Social Media Sharing Prompts In A NativeScript Angular Application

Social media can be huge towards building your brand and promoting your mobile application. It can also make a difference in the user experience of your application. Allowing users to share text and media on their own social media profiles could be huge, no matter how you look at it. A while back I had demonstrated social media sharing in a vanilla NativeScript application for iOS and Android. While vanilla NativeScript is still a very valid option, I’ve been going the route of Angular lately, so I figured this topic could be revisited.

We’re going to see how to implement social media sharing features in a NativeScript mobile application build with Angular.

Going into this, you should probably take note that an emulator won’t get you very far when it comes to this topic. This is because the emulators generally don’t have any share targets ready to go. For example, take the following animated image:

You’ll notice that we can share text or images, but the two emulators don’t have options like Twitter or Facebook. This is because the emulators don’t have any social platforms installed. You can only share to what is available on the device.

Creating a New NativeScript Project with Social Media Sharing

Our project won’t be very complex, but it will get the job done. For simplicity, we’re going to start with a fresh project that uses Angular and the social media sharing features.

From the Terminal (Mac and Linux) or Command Prompt (Windows), execute the following:

Implementing the Application Logic and Page UI

The development of our application will consist of two parts, the TypeScript logic layer and the HTML UI layer. Starting with the TypeScript portion, open the project’s app/app.component.ts file and include the following code:

You’ll notice that inside my AppComponent class I have two methods, one for sharing text and one for sharing images. Taking a look at the image sharing part, you can see we are loading an image from a URL. This is not the only way to accomplish sharing images. We can also share images in files by using fromFile instead of the fromUrl method.

Now let’s have a look at our very basic HTML. Open the project’s app/app.component.html file and include the following HTML markup:

In the above example we have a very basic action bar and a vertically stacked layout of two buttons. When either of the buttons are pressed, their corresponding TypeScript methods are called.

That is all there is to it!

Conclusion

You just saw how to implement social media sharing features in a NativeScript iOS and Android application built with Angular. These features will allow users to share text and images via whatever social platforms they have installed on their device. Such platforms include, but are not limited to, Facebook, Twitter, and SMS. If you’re not interested in Angular, you might check out my vanilla article on the subject.

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.