> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# LynxShareTarget

Receives what the person shares to the app from another app's share sheet: text, a link, or files such as photos. [`LynxShare`](/api/angular-lynx/sharing/share.md) 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:

```json title="angular.json"
"options": {
  "shareTarget": { "mimeTypes": ["text/plain", "text/uri-list", "image/*"] }
}
```

[The `shareTarget` option](#the-sharetarget-option) below says how iOS offers each type.

What it adds:

- **iOS:** a [Share Extension](/guide/glossary.md#share-extension), and an [App Group](/guide/glossary.md#app-group) it shares with the app. The group is `group.` and the app's `appId`, in the app's and the extension's entitlements.
- **Android:** `ACTION_SEND` and `ACTION_SEND_MULTIPLE` intent 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.

:::warning
**The App Group needs a team in the paid Apple Developer Program.** A free [Personal Team](/guide/glossary.md#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](/guide/native-apps/device-features.md) 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:

```typescript
import { Service, inject, signal } from '@angular/core';
import { LynxShareTarget, type ReceivedShare } from '@blotch/angular-lynx';

@Service()
export class Inbox {
  readonly items = signal<readonly ReceivedShare[]>([]);

  constructor() {
    inject(LynxShareTarget).onShare((share) =>
      this.items.update((items) => [...items, share]),
    );
  }
}

// A service is only made when something injects it: the root component
// injects Inbox, so the handler is there as the app starts.
export class App {
  readonly #inbox = inject(Inbox);
}
```

A file arrives as a `Blob` with its name, the shape [`LynxFilePicker`](/api/angular-lynx/media/file-picker.md) picks files as. So you can:

- send it as a request's body: `http.post(url, file.blob)` sends its `type` as the `Content-Type`
- share it on with `LynxShare`, as it is

## API

### `LynxShareTarget`

```typescript
@Injectable({ providedIn: 'root' })
class LynxShareTarget {}
```

| Host                                                                               | How the app is a share target                                                                                                                             |
| ---------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| iOS (`ng build --platform ios`)                                                    | A [Share Extension](/guide/glossary.md#share-extension), which copies the share to an [App Group](/guide/glossary.md#app-group), opens the app and closes |
| Android (`ng build --platform android`)                                            | `ACTION_SEND` and `ACTION_SEND_MULTIPLE` intent filters on the app's activity                                                                             |
| Web preview, Lynx Explorer, an app without `shareTarget`                           | None: no share ever arrives                                                                                                                               |
| Unit tests ([testing library](/api/testing-library/fakes/sharing.md#receiveshare)) | `receiveShare()`                                                                                                                                          |

The phone brings the app to the front, starting it if it has to, and then the share arrives here.

### `LynxShareTarget.onShare()`

```typescript
onShare(
  handler: ShareTargetHandler,
  options?: { readonly injector?: Injector },
): () => void;
```

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 `injector` to tie it to another. Outside one, it stays until it's removed.
- **Returns** a function that removes the handler.

### `ReceivedShare`

```typescript
type ReceivedShare = {
  readonly files: readonly ReceivedShareFile[];
  readonly title?: string;
  readonly text?: string;
  readonly url?: string;
};

type ReceivedShareFile = {
  readonly blob: Blob;
  readonly name: string;
  readonly type: string;
  readonly size: number;
};

type ShareTargetHandler = (share: ReceivedShare) => void | Promise<unknown>;
```

It's a [`ShareData`](/api/angular-lynx/sharing/share.md#sharedata), with what the sharing app gave of it:

| Field   | Description                                                                                                    |
| ------- | -------------------------------------------------------------------------------------------------------------- |
| `files` | The shared files of the types the app takes, each with its name, MIME type and size. Empty when there are none |
| `title` | The share's subject, such as a page's title from the browser, where the sharing app gives one                  |
| `text`  | Shared text, such as a selection or a note. Several texts in one share arrive joined by line breaks            |
| `url`   | A shared link, when the app takes `text/uri-list` (listed, or through `text/*` or `*/*`). See below            |

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 as `https://…`) has it in `url` instead. Other text, including a `mailto:` or `tel:` link, stays text. An app that doesn't take `text/plain` doesn't get it.
- **An app that takes `text/plain` but not `text/uri-list`** gets a link as its `text`, 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 `mimeTypes` doesn't list.**
- **A share left with nothing** doesn't arrive at all.

## The `shareTarget` option

| Field       | Type       | Description                                                                                                                                                        |
| ----------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `mimeTypes` | `string[]` | Required. The types the app takes: `text/plain` for text, `text/uri-list` for links, `image/*` for photos, `video/*`, or any other type, such as `application/pdf` |

- **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 `mimeTypes` asks. 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.

## Related

- [`LynxShare`](/api/angular-lynx/sharing/share.md)
- [Build options](/api/build/index.md#native-app-options)
