

Sharing Files from iOS 15 Safari to Apps using Web Share
source link: https://blog.bitsrc.io/sharing-files-from-ios-15-safari-to-apps-using-web-share-c0e98f6a4971
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sharing Files from iOS 15 Safari to Apps using Web Share
Safari 15 adds support for Web Share level 2
There’s so much to unpack in the Safari 15 beta which was announced at WWDC this week but one standout for me personally is the adoption of Web Share level 2. Finally, web developers will have an easy to way to share files from Safari to applications using the native share dialogue. I can’t begin to explain how important this is for the web. Up until now, getting a file from a web app to a native app was not easy. I’ll give you a scenario.
Let’s say I’ve built a web app that generates dynamic images and I’d like to share that image to Twitter. Currently, I would instruct the user to download the image, either using the “press and hold” gesture or by utilizing the recently introduced Safari download functionality. In either case, the image usually ends up in the user’s photo library and it is up to the user to then go to that file and share it on Twitter. Wouldn’t it make more sense to simply share the image directly to Twitter?
Here’s another scenario. iOS 14.3 added support for the MediaRecorder API which allows us to dynamically record new videos and audio in the browser. What if I built a bespoke camera app which recorded a video of the user and wanted to share that video on Instagram stories? In the past, I’ve built systems that require users to email the video to themselves, download it from their email app to their photo library, and finally share it to stories. Wouldn’t it be nice if I could simply share that dynamic video straight to Instagram?
This is what Web Share level 2 attempts to remedy. Let’s take a look at how it works.
Using Web Share level 2 to Share Files
Web Share level 1 only allowed us to share links but it is helpful to look back on that syntax to understand how easy it is to add file sharing. For example, if I was going to build out a button which shared my dev blog, it would look like this.
if (navigator.canShare) {
navigator.share({
title: 'Lee Martin',
text: 'Netmaker. Playing the Internet in your favorite band for nearly two decades.',
url: 'https://leemartin.dev'
})
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
}
When a user clicks on this button, the native share dialogue would appear along with all of their apps which allow link sharing. From here, the user can easily choose where they would like to share the url.
Now, what if I wanted to add an image to this share? That’s where Web Share level 2 comes in. First, we’ll want to create an array of the files as blobs we wish to share. In this example, I would only be including a single image. My image generation apps typically use <canvas>
to generate images so I can use the toBlob()
method to turn the dynamic canvas into an image blob and then initialize a file out of it. Finally, I can place that file into an array.
canvas.toBlob(blob => {
let file = new File([blob], "image.jpg", {
type: "image/jpeg"
}) let files = [file]
}, "image/jpeg")
I can then pass this files array to Web Share along with my other data.
if (navigator.canShare && navigator.canShare({ files: files })) {
navigator.share({
title: 'Lee Martin',
text: 'Netmaker. Playing the Internet in your favorite band for nearly two decades.',
url: 'https://leemartin.dev',
files: files
})
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
}
Yep, all we need to do is add the files
parameter. In addition, we can make sure the browser is capable of sharing the files by using the canShare()
method. And that's about it. I would just suggest familiarizing yourself with the File constructor methods so you can turn your images, videos, and other content into the required format.
What next?
If you’re curious about how WebRTC, MediaRecorder, and Web Share level 2 can all come together in the same app, check out this CodePen I put together of a full camera user experience. Note: This will only work on the Safari 15 beta at the moment. Want to learn how we got here in the first place? Check out this Twitter thread. Happy hacking!
Recommend
-
37
Dare to share: best practices for sharing documents in G Suite 2019-06-12adminGoogleCloud
-
37
I implemented the Web Share API over on The Session back when it was first available in Chrome in Android. It’s a nifty and
-
10
How to Upload and Share Files From the Terminal Using Transfer.sh By Nitin Ranganath Published 1 hour ago...
-
5
6 Apps and Files That Are Probably Using Up All Your Mac’s Storage By Lucus Newman Published 13 hours ago Running out o...
-
4
RESEARCHUsing AI to compress audio files for quick and easy sharing October 25, 2022
-
4
How to Save or Share a Safari Web Page as a PDF in iOS 13Friday June 14, 2019 12:58 PM PDT by Tim Hardwick
-
11
Ask other apps for photos, files and more using ActivityResultContracts
-
2
Thursday June 8, 2023 8:44 am PDT by Tim HardwickThe first macOS Sonoma beta was released to developers and early adopters this week, and cha...
-
5
Safari hits record market share while Chrome slips, Edge holds steady Google still owns over 60 percent of the browser market, so the real race is between Apple and Microsoft
-
2
Smaller indie web browsers eat into Safari’s EU share after DMA takes effect Wednesday, April 10, 2024 2:01 pmWednesday, April 10, 2024
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK