Read more

JavaScript: Sharing content with the native share dialog

Tobias Kraze
October 18, 2018Software engineer at makandra GmbH

Mobile Chrome and Safari support the "web share API" which allow you to use the native share functionality of an Android or iOS phone. Some desktop OSs like Windows or MacOS also support native share dialogs. See Can I Use Show archive.org snapshot for a detailed support matrix.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

When clicking a share button using this API, the browser will automatically show all installed applications that support content sharing, such as Whatsapp, Facebook, Twitter, e-mail etc.

The API is extremely simple to use:

if (typeof navigator.share === 'function') {
  let data = {
    url: '<url to share>',
    title: '<title to share>',
    text: '<text to share>',
  }
  element.addEventListener('click', (event) => {
    navigator.share(data)
    event.preventDefault()
  })
}

If you require feedback, navigator.share will return a promise that resolves or rejects, depending on whether the sharing was completed.

Note that this will only work on https pages, so to test it in development, you will have to start your development server with SSL.

Posted by Tobias Kraze to makandra dev (2018-10-18 18:14)