简体   繁体   English

如何从网页上的链接打开本机 iOS 共享模式?

[英]How to open the native iOS share modal from a link on a webpage?

I have a share button on my webpage.我的网页上有一个share button I want tapping the button in mobile Safari to open the iOS native share modal.我想点击移动 Safari 中的按钮来打开 iOS 本机共享模式。

How do I achieve this with HTML/Javascript?我如何使用 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.我无法在 Stack Overflow 或 Google 上找到任何答案。

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 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'); shareDialog.classList.add('is-open'); It opens your alternative share panel.它会打开您的替代共享面板。

More info about native share API support and features you can read there您可以在此处阅读有关本机共享 API 支持和功能的更多信息

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/还可以在这里查看更多信息https://web.dev/web-share/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM