The Web Share API in Safari on iOS 
Tags: ios, webshare, safari, javascript
Published: 2019-10-28 15:46:03

*This was originally posted <a href="https://adactio.com/journal/15972" rel="canonical">on my own site</a>.*

I implemented [the Web Share API](https://w3c.github.io/web-share/) over on [The Session](https://thesession.org/) back when it was first available in Chrome in Android. It’s a nifty and [quite straightforward API](https://css-tricks.com/how-to-use-the-web-share-api/) that allows websites to make use of the “sharing drawer” that mobile operating systems provide from within a web browser.

I already had sharing buttons that popped open links to Twitter, Facebook, and email. You can see these sharing buttons on individual pages for [tunes](https://thesession.org/tunes/12), [recordings](https://thesession.org/recordings/6299), [sessions](https://thesession.org/sessions/6655), and so on.

I was already intercepting clicks on those buttons. I didn’t have to add too much to also check for support for the Web Share API and trigger that instead:

```
if (navigator.share) {
  navigator.share(
    {
      title: document.querySelector('title').textContent,
      text: document.querySelector('meta[name="description"]').getAttribute('content'),
      url: document.querySelector('link[rel="canonical"]').getAttribute('href')
    }
  );
}
```

That worked a treat. As you can see, there are three fields you can pass to the `share()` method: `title`, `text`, and `url`. You don’t have to provide all three.

Earlier this year, [Safari on iOS shipped support for the Web Share API](https://webkit.org/blog/8718/new-webkit-features-in-safari-12-1/). I didn’t need to do anything. ’Cause that’s how standards work. You can make use of APIs before every browser supports them, and then your website gets better and better as more and more browsers add support.

But I recently discovered something interesting about the iOS implementation.

When the `share()` method is triggered, iOS provides multiple ways of sharing: Messages, Airdrop, email, and so on. But the simplest option is the one labelled “copy”, which copies to the clipboard.

Here’s the thing: if you’ve provided a `text` parameter to the `share()` method then that’s what’s going to get copied to the clipboard—not the URL.

That’s a shame. Personally, I think the `url` field should take precedence. But I don’t think this is a bug, per se. There’s nothing in the spec to say how operating systems should handle the data sent via the Web Share API. Still, I think it’s a bit counterintuitive. If I’m looking at a web page, and I opt to share it, then surely the URL is the most important piece of data?

I’m not even sure where to direct this feedback. I guess it’s under the purview of the Safari team, but it also touches on OS-level interactions. Either way, I hope that somebody at Apple will consider changing the current behaviour for copying Web Share data to the clipboard.

In the meantime, I’ve decided to update my code to remove the text parameter:

```
if (navigator.share) {
  navigator.share(
    {
      title: document.querySelector('title').textContent,
      url: document.querySelector('link[rel="canonical"]').getAttribute('href')
    }
  );
}
```

[If the behaviour of Safari on iOS changes](https://bugs.webkit.org/show_bug.cgi?id=203221), I’ll reinstate the missing field.

By the way, if you’re making progressive web apps that have `display: standalone` in the web app manifest, please consider using the Web Share API. When you remove the browser chrome, you’re removing the ability for users to easily share URLs. The Web Share API gives you a way to reinstate that functionality.

*This was originally posted <a href="https://adactio.com/journal/15972" rel="canonical">on my own site</a>.*