navigator.share(shareData) is not working

Viewed 877

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");
  }

}
1 Answers

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().

Related