0

I have a simple "share button" component in my React app that uses the Web Share API to share a link. Interestingly, in mobile safari it only seems to work a single time for a given pageload. Subsequent clicks on the share button result in the following error:

NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

The first click works fine, resulting in the native iOS share sheet appearing with the selection of share options. The subsequent failure occurs regardless of whether the initial share is cancelled or actioned.

Here's the relevant code that produces the issue:

export const SharerButton: React.FC<Props> = ({
  children,
  className,
  url,
  title,
  text,
  onShareSuccess = () => null,
  onShareCancel = () => null,
  kind = 'primary',
  successText = 'Link copied',
}) => {

  const handleShare = async () => {
    const canShare =
      (navigator.canShare !== undefined &&
        navigator.canShare({ title, text, url })) ||
      navigator.share !== undefined;

    if (!canShare) {
      // fallback share (copy to clipboard)
      return;
    }

    return navigator
      .share({ title, text, url })
      .then(() => onShareSuccess(true))
      .catch((error) => {
        console.error(error);
        onShareCancel(true);
      });
  };

  return (
    <Button
      className={className}
      kind={kind}
      icon="share"
      onClick={handleShare}
    >
      {children}
    </Button>
  );
};

Any idea why this could be happening?

I've managed to reproduce the issue in this simple CodeSandbox: https://xgxrn.csb.app/

iOS version: 14.1 Model: iPhone 11 Pro

harryg
  • 23,311
  • 45
  • 125
  • 198
  • 1
    Also seeing the same thing. First time I've ever used the Share API, so no idea if this is a recent issue or I'm doing something fundamentally wrong. Have tried async/non async, unbinding the click event then rebinding it again. Exactly the same error. – stevehayter Nov 09 '20 at 17:08
  • 1
    it seems that it is a known regression : https://bugs.webkit.org/show_bug.cgi?id=216913, waiting for the patch to be released... – Thibault Nov 17 '20 at 14:16
  • 1
    Does this answer your question? [Web Share API fails on second execution](https://stackoverflow.com/questions/64298234/web-share-api-fails-on-second-execution) – DenverCoder9 Mar 11 '21 at 20:24
  • 1
    no, in the code in my question I do catch any exception that's thrown. It was happening due to a webkit regression that affected some iOS devices . It's been patched in recent versions – harryg Mar 14 '21 at 20:11
  • This is still an issue on ios16 when trying to share files – heroicsatsuma Mar 09 '23 at 15:34

0 Answers0