Share Files and Links
On the web, navigator.share() opens the system's share sheet, so someone can send a photo or a link to another app. Lynx has no navigator, so LynxShare opens the phone's share sheet instead. It takes what navigator.share() takes: files, a title, text and a URL.
Share a photo your app made
A photo editor that draws on a <canvas> can export the result with toBlob() and share it:
share() resolves true once the person has shared, and false when they close the sheet without sharing.
Each file is a { blob, name } pair rather than a File: Lynx has no File class. The name is what the person sees, in AirDrop or Files, and the extension tells the other app the file's type when the Blob has none. A photo from LynxImagePicker is already this shape, so you can share it as it is: share({ files: [picked] }).
Share a link or text
The URL must be absolute, with its scheme: there's no page for a relative one to be relative to. The title becomes an email's subject, and the heading of the sheet's preview where the platform shows one.
Save to Photos
On iOS, the share sheet has Save Image for a JPEG, PNG or other image, which adds it to the photo library. The first time, iOS asks the person whether to allow it, with your app's reason: "Saves the images you choose to your photo library." Give your own with savePhotos in angular.json's permissions:
Android's share sheet has no Save Image: there, people save a file with an app such as Files or Google Photos.
Export a canvas
toBlob() reads the canvas's drawing buffer, which WebGL clears once a frame is on screen, as in a browser. So draw and call toBlob() in the same function, or create the context with preserveDrawingBuffer: true. Otherwise you get a transparent image. See Save it as an image.
What people see
- On iOS, the system share sheet (
UIActivityViewController), with AirDrop, Messages, Mail, Save Image, Save to Files and the other apps that take the files.share()resolvestrueonce the person finishes, such as when the photo is saved or the message sent. - On Android, the system share sheet. Android doesn't tell an app whether the person finished sharing in the app they picked, so
share()resolvestrueas soon as they pick an app, andfalsewhen they close the sheet without picking one. The sheet's own buttons, such as Copy, count as closing it:false. - In the web preview, the browser's share sheet, from
navigator.share(). A browser that can't share files, such as Firefox on a computer, downloads them instead, andshare()resolvestrue. Browsers share only in answer to a tap, so callshare()from a tap handler, with the files ready.
When it doesn't share
Closing the share sheet isn't an error: share() resolves false. It rejects with a TypeError when there's nothing to share, a file has no name or the URL isn't absolute, and with a ShareError, whose code says why, when the sheet can't open:
Lynx Explorer has no share sheet, so there share() rejects with unavailable. Try it on a simulator, emulator or phone with ng serve --platform ios or --platform android, or in the web preview. To use it in your own native app, see Embed in a Native App.
Test it
In a spec there's no share sheet to tap through, so provide a stand-in that records what was shared:
Return false from the stand-in to test what happens when the person closes the sheet. Without a stand-in, share() rejects with unavailable, as in Lynx Explorer.
