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

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

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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745649600a4638180.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信