• Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
No Result
View All Result

How to use Web Share API in your Angular applications

by Shashank Tiwari
August 8, 2018
in Angular
2
3 Minutes Read
How to use Web Share API in your Angular applications

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.

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. Installingng-navigator-sharemodule 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 inAppComponentclass.

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 theNgNavigatorShareServiceservice fromng-navigator-share.
  • Then createngNavigatorShareServiceproperty in your component class, havingNgNavigatorShareServiceas an interface.
  • In your constructor method you will pass thengNavigatorShareServiceas parameter, then you apply this parameter to the property ofAppComponentclass as shown below,
    constructor(
    ngNavigatorShareService: NgNavigatorShareService
    ) {
    this.ngNavigatorShareService = ngNavigatorShareService;
    }
  • Lastly, you haveshareApi()method, which is an async method and consuming theshare()method ofngNavigatorShareServiceservice.
    As I said earliershare()method returns Promise, so I am using async/await. You can usethen()/catch(), won’t make any difference.
  • Theshare()method will expect three parameterstitle,textandurl.
  • Here titleis a mandatory parameter and you have pass eithertextorurlparameter. 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!

Tags: NotificationsWeb Share API
Previous Post

Creating API Rate limiter in Nodejs using express and Redis

Next Post

Detect Faces in Images using Nodejs

Related Posts

Implementing Swipe to delete in Angular
Angular

Implementing Swipe to delete in Angular

August 8, 2018
Building a 2D racing Game using Angular
Angular

Building a 2D racing Game using Angular

August 8, 2018
Multiplayer Tic-Tac-Toe Game using Angular, Nodejs and socket
Angular

Building a Dead-Simple Multiplayer Tic-Tac-Toe Game using Angular, Nodejs and socket.io Rooms: Part 2

August 8, 2018
Next Post
Detect Faces in Images using Nodejs

Detect Faces in Images using Nodejs

Comments 2

  1. Ata says:
    5 years ago

    I get this error on all kind of browsers:
    {shared: false, error: “This service/api is not supported in your Browser”}

    Reply
    • Shashank Tiwari says:
      5 years ago

      Hi, Are you using this package on Desktop browser?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




https://codershood.info

www.codershood.info programming blog dedicated to providing high-quality coding tutorial and articles on web development, Angular, React, Laravel, AngularJs, CSS, Node.js, ExpressJs and many more. We also provide ebook based on complicated web application along with the source code.

  • Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.

No Result
View All Result
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.