How to use Web Share API in your Angular applications

With the launch of Web Share API, Sharing Progressive web applications and Mobile first web applications became a piece of cake for developers. Using Web Share API you can implement native sharing capabilities in your application. Am sure you have used it in one of your application if you create Progressive web applications, Mobile first web applications or Mobile focused Websites. But the real pain comes when you have to write the same code again and again.

Well not anymore, I have created Angular Wrapper for Web Share API. The reason I created this API was just the other day I was copying the code of this API probably for 7th or 8th time, Seriously !!! So created this wrapper and it’s called ng-navigator-share. I hope this is useful to you. In this article, I will show how can you use this wrapper in your Angular applications.

  View on NPM

  View On Yarn

  View On Github

  Demo

Make sure you open demo on a mobile browser

 




 

1. What is ng-navigator-share?

It’s a lightweight wrapper on top of Web Share API, Which returns Promise also It has only one dependency. It is incredibly easy to use, which we will see down the road. You will have to call only one method i.e. share() method on the object of this wrapper and then this wrapper will take care of rest of things. So under the hood, it will basically check the if  Web Share API is available on your browser that’s it.

2. Setting up our Application

Let’s use Angular CLI to setup our application. If you don’t have Angular CLI installed on your machine run the below command to install it globally.
npm install -g angular-cli
After Angular CLI installation, to create a new Angular project Run below command. This command will create all the necessary files, download all the required external dependencies and do all of the setup work for us.

ng new AppName

3. Installing ng-navigator-share module from NPM

You can use either the npm or yarn command-line tool to install packages. Use whichever is appropriate for your project in the examples below.

NPM

YARN

4. Consuming API in Angular Application

Now the action time, open the app.component.html and write the below markup. I assume below code needs no explanation, so we will go ahead and write some code in AppComponent class.

app.component.html:

Open the app.component.ts and paste the below code. In the below code, we will consume this module. Which will give the native capabilities to share the application.

app.component.ts:

Explanation:

  • First thing first, you need to include the NgNavigatorShareService service from ng-navigator-share.
  • Then create ngNavigatorShareService property in your component class, having NgNavigatorShareService as an interface.
  • In your constructor method you will pass the ngNavigatorShareService as parameter, then you apply this parameter to the property of AppComponent class as shown below,
  • Lastly, you have shareApi() method, which is an async method and consuming the share() method of ngNavigatorShareService service.
    As I said earlier share() method returns Promise, so I am using async/await. You can use then()/catch(), won’t make any difference.
  • The share() method will expect three parameters titletext and url.
  • Here  title is a mandatory parameter and you have pass either text or url parameter. If you won’t, you get a warning in console.

5. Contribution

I welcome you to fork and add more features into it. If you have any bugs or feature request, please create an issue at Github repository.

6. Final thoughts

This article was short and sweet, actually, it was an introduction to the ng-navigator-share module. I personally find this module useful, so instead of copying and pasting the same code, again and again, I write these few lines and I get done.

I would suggest you, to use this module, and let me know what you think. Also if you are interested to add your inputs, I welcome you mail me on my email or just comment below, I would love to hear from you.

Till then, Happy Sharing!