最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to open the native iOS share modal from a link on a webpage? - Stack Overflow

matteradmin7PV0评论

I have a share button on my webpage. I want tapping the button in mobile Safari to open the iOS native share modal.

How do I achieve this with HTML/Javascript?

I can see that the Wall Street Journal have been able to achieve this with the share buttons on their website. I have not been able to find any answers on Stack Overflow or Google.

I have a share button on my webpage. I want tapping the button in mobile Safari to open the iOS native share modal.

How do I achieve this with HTML/Javascript?

I can see that the Wall Street Journal have been able to achieve this with the share buttons on their website. I have not been able to find any answers on Stack Overflow or Google.

Share Improve this question edited Apr 4, 2019 at 18:56 YK S 3,4405 gold badges29 silver badges58 bronze badges asked Apr 4, 2019 at 17:38 gcg1gcg1 812 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You could try

navigator.share({
 title: 'Your title',
 text: 'Your text',
 url: 'Your url to share'
})

on click on your link.

You can use feature

navigator.share

But not all browsers support it. In this case you can check it by using this construction:

if (navigator.share) {
  title: ' ',
  text: ' ',
  url: ' '
} else {
  // Fallback
}

For example use this code

   shareButton.addEventListener('click', event => {
      if (navigator.share) {
        navigator.share({
          title: 'WebShare API Demo',
          url: ' '
        }).then(() => {
          console.log('Thanks for sharing!');
        })
        .catch(console.error);
      } else {
        shareDialog.classList.add('is-open');
      }
    });

Where

shareDialog.classList.add('is-open'); It opens your alternative share panel.

More info about native share API support and features you can read there

if (navigator.share) {
  navigator.share({
    title: 'web.dev',
    text: 'Check out web.dev.',
    url: 'https://web.dev/',
  })
    .then(() => console.log('Successful share'))
    .catch((error) => console.log('Error sharing', error));
}

also check here for more info https://web.dev/web-share/

Post a comment

comment list (0)

  1. No comments so far