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

# LynxLinking

Opens links outside the app, in the browser, in another app, or in a browser sheet over the app, and hears the links that open this one. `import { LynxLinking, LinkingError, type OpenInAppOptions, type LinkingErrorCode, type LynxLinkHandler } from '@blotch/angular-lynx';`

[Open links outside the app](/guide/open-links.md) and [Open the app from a link](/guide/deep-links.md) show it in use.

## Usage

```typescript
const linking = inject(LynxLinking);

await linking.openInApp('https://example.com/help');
await linking.openURL('mailto:support@example.com');
const hasWhatsApp = await linking.canOpenURL('whatsapp://send');

const startedWith = linking.getInitialURL(); // 'myapp://orders/42', or null
linking.onURL((url) => console.log('Opened with', url));
```

## API

### `LynxLinking`

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

| Host                                                                     | `openURL()`                | `openInApp()`              | `canOpenURL()`             |
| ------------------------------------------------------------------------ | -------------------------- | -------------------------- | -------------------------- |
| iOS (`ng build --platform ios`)                                          | `UIApplication.open`       | `SFSafariViewController`   | `UIApplication.canOpenURL` |
| Android (`ng build --platform android`)                                  | An `ACTION_VIEW` intent    | A Custom Tab               | `resolveActivity`          |
| Web preview                                                              | A new tab                  | A new tab                  | `true`                     |
| Lynx Explorer                                                            | Rejects with `unavailable` | Rejects with `unavailable` | `false`                    |
| Unit tests ([testing library](/api/testing-library/index.md#openedurls)) | Records the URL            | Records the URL            | `true`                     |

Links to the app arrive only in the apps `--platform` builds (`ng build` or `ng serve`), from a scheme in angular.json's [`deepLinkSchemes`](/api/build/index.md#native-app-options). In the web preview and Lynx Explorer, [`getInitialURL()`](#lynxlinkinggetinitialurl) is `null` and no [`onURL()`](#lynxlinkingonurl) handler is called; in unit tests, the testing library's [`render()`](/api/testing-library/index.md#rendercomponent-options) starts the app with one (its `initialURL` option), and [`openLink()`](/api/testing-library/index.md#openlink) sends one while it's open.

### `LynxLinking.openURL()`

```typescript
openURL(url: string): Promise<void>;
```

Opens `url` where the system opens it: the default browser for `http` and `https`, the mail app for `mailto:`, the phone for `tel:`, and another app for its own scheme. An http(s) link that an installed app claims, through a universal link on iOS or an App Link on Android, opens that app. Resolves once the URL is handed over. Rejects with a [`LinkingError`](#linkingerror) when nothing on the device opens it, and with a `TypeError` when `url` isn't absolute.

### `LynxLinking.openInApp()`

```typescript
openInApp(url: string, options?: OpenInAppOptions): Promise<void>;
```

Opens an `http` or `https` `url` in a browser sheet over the app, so the person comes straight back to where they were. Resolves once they're back in the app:

- **iOS:** when they close the sheet.
- **Android:** when the app comes back to the front. Android doesn't say when a Custom Tab closes, so this is the closest it can tell: the person closed the tab, or switched back to the app another way, which leaves the tab behind it.
- **Web:** when they close the tab or come back to the app's tab.

Rejects with a `TypeError` for any other scheme, which the sheets can't show (use `openURL()` for those), and with a [`LinkingError`](#linkingerror) when the sheet can't open, such as while another is open.

The sheet keeps its own cookies on iOS, apart from Safari's. On Android it shares the browser's. To sign in through a page and get a result back, use [`LynxWebAuthSession`](/api/angular-lynx/web-auth-session.md) instead.

### `LynxLinking.canOpenURL()`

```typescript
canOpenURL(url: string): Promise<boolean>;
```

Resolves whether something on the device opens `url`. iOS answers only for the schemes the app declares, and Android 11 and later only for the apps the app says it looks for, so list the schemes you ask about in [`canOpenURLSchemes`](/api/build/index.md#native-app-options) in angular.json. `http`, `https`, `mailto`, `tel` and `sms` are always declared. For any other scheme, the answer is `false`.

### `LynxLinking.getInitialURL()`

```typescript
getInitialURL(): string | null;
```

The link that started the app, such as `myapp://orders/42`, or `null` when the person started it another way, from its icon for instance. It stays the same for as long as the app runs. React Native's `Linking.getInitialURL()` is asynchronous; this answers at once, because the app is given the link before it starts. With [`provideRouter()`](/api/angular-lynx/routing.md#providerouter), the router's first screen is already the link's route.

### `LynxLinking.onURL()`

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

type LynxLinkHandler = (url: string) => void;
```

Calls `handler` with each link that opens the app while it's running, in front or in the background, as React Native's `url` event does. The link that started the app isn't one of them: read it with `getInitialURL()`. Links that arrived while the app was still starting wait for the first handler. Called from an injection context, such as a constructor, the handler is removed when its component or service is destroyed; pass `injector` to tie it to another one. The function it returns removes it sooner. A handler's error goes to the app's `ErrorHandler`, and the other handlers are still called.

With [`provideRouter()`](/api/angular-lynx/routing.md#providerouter), the router navigates to each link's route as well; [`provideDeepLinkRoute()`](/api/angular-lynx/routing.md#providedeeplinkroute) changes that.

### `OpenInAppOptions`

```typescript
type OpenInAppOptions = {
  readonly toolbarColor?: string;
  readonly controlsColor?: string;
};
```

| Option          | Description                                                                                      |
| --------------- | ------------------------------------------------------------------------------------------------ |
| `toolbarColor`  | The color of the sheet's toolbar. On Android, the tab picks its icons' color to contrast with it |
| `controlsColor` | The color of the sheet's buttons. iOS only                                                       |

Colors are hex (`#1e3a8a`, `#1e3a8a80`, `#fff`) or `rgb()`/`rgba()` with commas. Anything else rejects with a `TypeError`. iOS 27 draws Safari's own glass buttons over the page and leaves both colors out; earlier versions use them.

### `LinkingError`

```typescript
class LinkingError extends Error {
  readonly code: LinkingErrorCode;
}

type LinkingErrorCode = 'unavailable' | 'failed';
```

| `code`        | When                                                                                                      |
| ------------- | --------------------------------------------------------------------------------------------------------- |
| `unavailable` | The host can't open links. Only the apps `--platform` builds (`ng build` or `ng serve`) can               |
| `failed`      | Nothing on the device opens the URL, a browser sheet was already open, or the browser blocked the new tab |
