With the launch ofWeb 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 calledng-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.
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. Installingng-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
npm install --save ng-navigator-share
YARN
yarn add --save ng-navigator-share
4. Consuming API in Angular Application
Now the action time, open theapp.component.htmland write the below markup. I assume below code needs no explanation, so we will go ahead and write some code inAppComponent
class.
app.component.html:
<!-- Web Share API Angular applications @author Shashank Tiwari --> <u> <strong (click)='shareApi()'>share</strong> </u>
Open theapp.component.tsand 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:
/* Web Share API Angular applications @author Shashank Tiwari */ import { Component } from '@angular/core'; import { NgNavigatorShareService } from 'ng-navigator-share'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { private ngNavigatorShareService: NgNavigatorShareService; constructor( ngNavigatorShareService: NgNavigatorShareService ) { this.ngNavigatorShareService = ngNavigatorShareService; } async shareApi() { try{ const sharedResponse = await this.ngNavigatorShareService.share({ title:'`Web Articles and Tutorials', text: 'Check out my blog — its worth looking.', url: 'www.codershood.info' }); console.log(sharedResponse); } catch(error) { console.log('You app is not shared, reason: ',error); } } }
Explanation:
- First thing first, you need to include the
NgNavigatorShareService
service fromng-navigator-share
. - Then create
ngNavigatorShareService
property in your component class, havingNgNavigatorShareService
as an interface. - In your constructor method you will pass the
ngNavigatorShareService
as parameter, then you apply this parameter to the property ofAppComponent
class as shown below,constructor( ngNavigatorShareService: NgNavigatorShareService ) { this.ngNavigatorShareService = ngNavigatorShareService; }
- Lastly, you have
shareApi()
method, which is an async method and consuming theshare()
method ofngNavigatorShareService
service.
As I said earliershare()
method returns Promise, so I am using async/await. You can usethen()/catch()
, won’t make any difference. - The
share()
method will expect three parameterstitle
,text
andurl
. - Here
title
is a mandatory parameter and you have pass eithertext
orurl
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 atGithub repository.
6. Final thoughts
This article was short and sweet, actually, it was an introduction to theng-navigator-sharemodule. 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 onmy emailor just comment below, I would love to hear from you.
Till then, Happy Sharing!
I get this error on all kind of browsers:
{shared: false, error: “This service/api is not supported in your Browser”}
Hi, Are you using this package on Desktop browser?