Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript - navigator.share(shareData) is not working

I just want to activate native apps, when clicking on the "share" button on my PWA (Progressive Web App) but it shows the following error

TypeError: Cannot read property 'apply' of undefined

I am using Nuxt Js

This is My code

function tshareData(i) {
  if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(
     navigator.userAgent)
  ) {
    try {
      let y = document.getElementById("sharetable" + i).innerText;
      const shareData = {
        title: "List",
        text: y,
        url: window.location.href
      };
      navigator
        .share(...shareData)
        .then(() => console.log("Successful share"))
        .catch(error => {
          console.log(error);
        });
    } catch (error) {
      console.log("Catch Error", error);
    }
  } else {
    alert("Browser Not Suported");
  }

}
question from:https://stackoverflow.com/questions/65875069/navigator-sharesharedata-is-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In general, you should use feature detection via

if (navigator.share) {
  // Web Share is available.
}

before attempting to use the Web Share API. Detection based on navigator.userAgent string matching is not a good idea.

As for the actual error you're reporting, it doesn't necessarily sound like it's coming from the usage of the Web Share API, as none of the code in your snippet actually calls an apply() method on anything. You should take a look at the stack trace associated with the exception and figure out what underlying code is actually calling apply().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...