LynxShareTarget
Receives what the person shares to the app from another app's share sheet: text, a link, or files such as photos. LynxShare is the other direction, sending from the app. import { LynxShareTarget, type ReceivedShare, type ReceivedShareFile, type ShareTargetHandler } from '@blotch/angular-lynx';
Turn it on
Add shareTarget to the build target's options in angular.json, with the MIME types the app takes:
The shareTarget option below says how iOS offers each type.
What it adds:
- iOS: a Share Extension, and an App Group it shares with the app. The group is
group.and the app'sappId, in the app's and the extension's entitlements. - Android:
ACTION_SENDandACTION_SEND_MULTIPLEintent filters on the app's activity, for those types. - Both: the native code behind
LynxShareTarget.
Without it, or with false, the app is in no share sheet. The native code, Android's intent filters, the Share Extension and its App Group are left out. Nothing rejects: no share ever arrives, so no onShare() handler is called.
The App Group needs a team in the paid Apple Developer Program. A free Personal Team can't sign it, and the build says so. Build for an iPhone with ios.teamId set to a paid team. The Simulator app needs no team.
See Turn on device features for every setting.
Usage
Register a handler from a service or component that's created as the app starts, such as a service the root component injects:
A file arrives as a Blob with its name, the shape LynxFilePicker picks files as. So you can:
- send it as a request's body:
http.post(url, file.blob)sends itstypeas theContent-Type - share it on with
LynxShare, as it is
API
LynxShareTarget
The phone brings the app to the front, starting it if it has to, and then the share arrives here.
LynxShareTarget.onShare()
Calls handler with each share the person sends the app.
- Every handler registered when the share arrives is called, in the order they were registered.
- A handler that throws, or returns a promise that rejects, is reported to Angular's
ErrorHandler. The others are still called. - Handlers registered as the service starts all get the share that started the app. The service takes the shares waiting on the phone a moment after it's created. By then, the handlers registered in the same constructor, or the same startup code, are all there.
- A share nothing handled waits. One that arrives while no handler is registered goes to the first handler registered, alone, once the code that registered it has finished.
- The handler goes with its injection context. Called from one, such as a constructor, the handler is removed when the component or service is destroyed. Pass
injectorto tie it to another. Outside one, it stays until it's removed. - Returns a function that removes the handler.
ReceivedShare
It's a ShareData, with what the sharing app gave of it:
A share can carry files along with text or a link.
How a link arrives:
- On Android, a link comes as text. A share whose text is one link with
://(such ashttps://…) has it inurlinstead. Other text, including amailto:ortel:link, stays text. An app that doesn't taketext/plaindoesn't get it. - An app that takes
text/plainbut nottext/uri-listgets a link as itstext, on both platforms.
What's left out:
- Files over 20 MiB in all. A share's files travel to JavaScript as base64, so together they're at most 20 MiB. A file that doesn't fit is left out.
- Anything of a type
mimeTypesdoesn't list. - A share left with nothing doesn't arrive at all.
The shareTarget option
- iOS's share sheet lists apps by kind, not MIME type. The extension's activation rule offers the app for text, links, images, videos or files, as
mimeTypesasks. Then the extension checks each file's own type against the list. - Any other type offers the app for every file. A type that isn't text, a link, an image or a video, such as
application/pdf, offers the app for any file. The extension then leaves out the files that aren't PDFs. - The extension shows little. It can't run the Angular app. It shows a card saying it's sending the share while it copies it, then opens the app.
- A share still arrives if iOS stops the extension opening the app. It waits, and arrives the next time the app comes to the front.
