Google's Web Share API Allows Link Sharing in Web Apps

Google's Web Share API Allows Link Sharing in Web Apps

Until now, only native mobile apps had the ability to share web links - not web apps, which could only share links through copying and pasting. However, as of Chrome 61, web apps can share links through the new Google's Web Share API. This API provides a method called navigator.share(), which invokes the native sharing routine of the host system.

Introduction to Google's Web Share API
The Web Share API is a Promise-based, single method interface developed by Google's Matt Gluca.
Requirements
To use the Web Share API, you must do the following:
  • Serve the page using only HTTPS
  • Invoke the method only in response to a user action, such as a mouse click, and not automatically through something like a page load
  • Feature-detect whether the sharing method exists in the current platform using code such as if (navigator.share == undefined)

Note: you can use the API with any URL, not just URLs belonging to your domain.

Usage
To invoke the share method, simply provide it an object that contains the following properties:

title: the title of the sharing dialog.
text: the text that will appear in the sharing dialog.
url: the url of the link to share.

Here's an example:
if (navigator.share) {
navigator.share({
title: 'Web Fundamentals for Developers',
text: 'Share this information',
url: 'https://developers.google.com/web',
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing', error));
}
Browser Compatibility
Currently, only Chrome for Android supports the Web Share API. It is not supported in any desktop browser, including Chrome implementations.
Future Enhancements
Future implementations will allow sites to register themselves as share receivers. This will enable sharing to the web instead of just from it.

The Web Share API provides a powerful native sharing feature for mobile apps developed specifically for Chrome, and is a must-learn interface for web developers.

Web Development APIs Tools Google