Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share button for mobile website to invoke iOS/Android system share dialogs

Is it possible to create a share button (link) in my website that can invoke share dialogs in iOS and Android systems?

I mean the following dialog for each system:

enter image description here

enter image description here

I am not asking of how to do it by using iOS/Android SDKs. I want it with only HTML/JavaScript.

like image 618
malhobayyeb Avatar asked Aug 24 '15 06:08

malhobayyeb


People also ask

What is navigator share?

The navigator. share() method of the Web Share API invokes the native sharing mechanism of the device to share data such as text, URLs, or files. The available share targets depend on the device, but might include the clipboard, contacts and email applications, websites, Bluetooth, etc.

What is shared API?

The Web Share API allows a site to share text, links, files, and other content to user-selected share targets, utilizing the sharing mechanisms of the underlying operating system. These share targets typically include the system clipboard, email, contacts or messaging applications, and Bluetooth or Wi-Fi channels.

What is a webshare?

Web sharing (also known as "screen sharing" and "desktop sharing") is a software function that allows a stream of data to be shared across the Internet to one or more devices. While the term can refer to the exchange of any kind of data, usually it refers to shared screen and audio data.


1 Answers

Use the Web Share API to share your URL on iOS or Android via Javascript.

It is supported by

  • Chrome since Version 61 (2016) and
  • iOS 12.2 is told to add support for it soon (2019).

$('#answer-example-share-button').on('click', () => {
  if (navigator.share) {
    navigator.share({
        title: 'Web Share API Draft',
        text: 'Take a look at this spec!',
        url: 'https://wicg.github.io/web-share/#share-method',
      })
      .then(() => console.log('Successful share'))
      .catch((error) => console.log('Error sharing', error));
  } else {
    console.log('Share not supported on this browser, do it the old way.');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id='answer-example-share-button'>Share!</button>

Citing Google Developers:

  • you must be served over HTTPS
  • you can only invoke the API in response to a user action, such as a click (e.g., you can't call navigator.share as part of the page load)
  • you can also share any URL, not just URLs under your website's current scope: and you may also share text without a URL
  • you should feature-detect it in case it's not available on your users' platform (e.g., via navigator.share !== undefined)
like image 90
flob Avatar answered Oct 12 '22 01:10

flob