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

# Connect native features

Let AngularLynx's sign-in, storage, links, shortcuts, photo picker and share sheet work in your own app. Each service calls a [**native module**](/api/angular-lynx/host/native-module.md) in the host app: Swift or Kotlin code your app registers with Lynx, which the runtime calls by name.

The apps `ng build --platform` makes have every module below. In your own app, register the ones you use.

| Service                                                                    | Native module                                         |
| -------------------------------------------------------------------------- | ----------------------------------------------------- |
| [`LynxWebAuthSession`](#sign-in-with-a-web-page)                           | `AngularLynxWebAuth`                                  |
| [`LynxLocalStorage`, `LynxSecureStorage`](#keep-data-after-the-app-closes) | `AngularLynxLocalStorage`, `AngularLynxSecureStorage` |
| [`LynxLinking`](#open-links-outside-the-app)                               | `AngularLynxLinking`                                  |
| [Deep links](#hand-over-the-links-that-open-the-app)                       | `AngularLynxLinking`, plus a global prop and an event |
| [`LynxShortcuts`](#hear-the-apps-shortcuts)                                | `AngularLynxShortcuts`                                |
| [`LynxImagePicker`](#let-someone-pick-a-photo)                             | `AngularLynxImagePicker`                              |
| [`LynxShare`](#open-the-share-sheet)                                       | `AngularLynxShare`                                    |

Each section lists the module's methods, the files to copy and what happens without it. The files are in AngularLynx's [`packages/native/templates`](https://github.com/Blotch-Smart-Frames/AngularLynx/tree/master/packages/native/templates).

Two terms come up below:

- **A callback** is the last argument of each method. It takes one map, your answer to JavaScript. Call it once per call.
- **A global event** is a message your native code sends to the LynxView, which the runtime listens for.

## Sign in with a web page

[`LynxWebAuthSession`](/guide/web-sign-in.md) calls a native module named `AngularLynxWebAuth`. Register one with a `start(request, callback)` method:

- **`request`** holds `url`, `callbackScheme` and `ephemeral`.
- **`callback`** takes one map. Call it once per `start()`:
  - `{ url }`: the whole URL the page redirected to on `callbackScheme`
  - `{ error, message }`, where `error` is `canceled` or `failed`

Copy it from the generated apps:

- **iOS:** `WebAuthModule.swift` uses `ASWebAuthenticationSession`.
- **Android:** `WebAuthModule.kt` opens a Custom Tab, and has the activity catch the redirect as a deep link.

Without the module, `start()` rejects with `unavailable`.

## Keep data after the app closes

[`LynxLocalStorage` and `LynxSecureStorage`](/guide/persistent-storage.md) call native modules named `AngularLynxLocalStorage` and `AngularLynxSecureStorage`. Register each with these methods:

- **`getItem(key, callback)`** calls back with `{ value }`, or `{}` if there's nothing stored under `key`.
- **`setItem(key, value, callback)`**, **`removeItem(key, callback)`** and **`clear(callback)`** call back with `{}` once they're done.
- **On failure,** any of them calls back with `{ error }`, a message, which the runtime rejects with.

Keys and values are strings: the runtime stores values as JSON. Call the callback once per call.

Copy them from the generated apps:

- **iOS:** `LocalStorageModule.swift` uses `UserDefaults`. `SecureStorageModule.swift` uses the Keychain.
- **Android:** `LocalStorageModule.kt` uses `SharedPreferences`. `SecureStorageModule.kt` encrypts each value with a key the Android Keystore holds.

Without the modules, both services keep values in memory, which lasts until the app closes.

## Open links outside the app

[`LynxLinking`](/guide/open-links.md) calls a native module named `AngularLynxLinking`. Register one with these methods:

- **`openURL(url, callback)`** opens `url` where the system opens it. It calls back with `{}` once it's handed over.
- **`openInApp(url, options, callback)`** shows an `http` or `https` `url` in a browser sheet over the app. It calls back with `{}` once the person is back.
- **`canOpenURL(url, callback)`** calls back with `{ value }`: `true` when something on the device opens `url`.
- **`openAppSettings(callback)`** opens the app's own page in the phone's Settings. It calls back with `{}` once it's handed over.
- **On failure,** any of them calls back with `{ error: 'failed', message }`.

Call the callback once per call. `openInApp()`'s `options` may hold `toolbarColor` and `controlsColor`. Each is a `#` and eight hex digits: red, green, blue and alpha.

List the schemes `canOpenURL` asks about:

- **iOS:** in your Info.plist's `LSApplicationQueriesSchemes`
- **Android:** in your manifest's `<queries>`

Copy it from the generated apps:

- **iOS:** `LinkingModule.swift` uses `UIApplication.open` and `SFSafariViewController`, and opens `UIApplication.openSettingsURLString` for Settings.
- **Android:** `LinkingModule.kt` sends an `ACTION_VIEW` intent and opens a Custom Tab, and an `ACTION_APPLICATION_DETAILS_SETTINGS` intent for Settings. It's given the activity as its param.

Without the module, `openURL()`, `openInApp()` and `openAppSettings()` reject with `unavailable`, and `canOpenURL()` resolves `false`.

## Hand over the links that open the app

`LynxLinking.getInitialURL()`, `onURL()` and the router's [deep links](/guide/deep-links.md) need the links your app is opened with. There are two cases.

### The link that started the app

Put it in the `initialURL` global prop, the whole URL as a string. (**Global props** is Lynx's name for [GlobalData](/guide/embedding/host-data.md#read-globaldata).) Send it before the bundle loads, and in every global props update after. The router starts at its route.

### A link while the app is open

1. **Keep the link** until the page takes it.
2. **Send the global event `angularLynxURL`** to the LynxView, with no parameters.
3. **Answer `takeURLs(callback)`.** The page calls it on the `AngularLynxLinking` module. It calls back with `{ urls }`, the links kept since the last call, oldest first, and forgets them.

The page also calls `takeURLs` once as it starts listening. So a link that came while it was loading isn't lost.

Copy it from the generated apps:

- **iOS:** `AppDelegate.swift` (the scene's `connectionOptions.urlContexts` and `scene(_:openURLContexts:)`), with `LinkingModule.swift`'s `LinkCenter`
- **Android:** `MainActivity.kt` (the intent in `onCreate` and `onNewIntent`), with `LinkingModule.kt`

Without them, `getInitialURL()` is `null` and no link reaches the page.

## Hear the app's shortcuts

[`LynxShortcuts`](/guide/shortcuts.md) tells a page that the person ran one of your app's shortcuts: an App Intent, a Home Screen quick action or an Android app shortcut. It calls a native module named `AngularLynxShortcuts`, and listens for a global event.

Your app:

1. **Keeps each shortcut that ran** until the page takes it, as `{ id }` or `{ id, route }`. One may run before the page has loaded, such as the shortcut that started the app.
2. **Sends the global event `angularLynxShortcut`** to the LynxView, with no parameters, each time it keeps one while the view exists.
3. **Answers `take(callback)`** with `{ shortcuts }`, the ones kept since the last call, oldest first, and forgets them. The page calls `take` on each event, and once as it starts.

Copy it from the generated apps:

- **iOS:** `ShortcutsModule.swift`. The generated App Intents hand their shortcut to its `ShortcutCenter`.
- **Android:** `ShortcutsModule.kt`, which `MainActivity` hands a shortcut's intent to.

Without the module, no shortcut ever reaches the page.

## Let someone pick a photo

[`LynxImagePicker`](/guide/image-picker.md) calls a native module named `AngularLynxImagePicker`. Register one with a `pick(options, callback)` method:

- **`options`** holds `maxDimension` and `quality` when the app passes them. With either, re-encode the photo as a JPEG, upright and scaled down to `maxDimension`.
- **`callback`** takes one map. Call it once per `pick()`:
  - `{ base64, name, type }`: the photo's bytes as base64, with a file name and a MIME type
  - `{ canceled: true }` when the person closed the picker
  - `{ error: "failed", message }`

Copy it from the generated apps:

- **iOS:** `ImagePickerModule.swift` uses `PHPickerViewController` and ImageIO.
- **Android:** `ImagePickerModule.kt` has the activity open the Photo Picker.

Without the module, `pick()` rejects with `unavailable`.

For `pick({ source: 'camera' })`, add a `takePhoto(options, callback)` method that opens the camera. It takes the same `options` and gives the same answers, plus:

- Re-encode the photo as an upright JPEG.
- Answer `{ error: "unavailable", message }` on a device without a camera.
- Answer `{ error: "denied", message }` when the person doesn't allow it.

The generated apps have it in an app with `permissions.camera`:

- **iOS:** `UIImagePickerController`. Your Info.plist needs `NSCameraUsageDescription`.
- **Android:** `ActivityResultContracts.TakePicture`, after asking for `CAMERA` if your manifest declares it.

Without `takePhoto`, the camera rejects with `unavailable`.

On Android, a `recover(callback)` method gives `LynxImagePicker.recover()` the photo picked or taken while Android recreated your activity, which no `pick()` was left to answer. Call `callback` once with the same answers as `pick`, or `{ canceled: true }` when there's none. The generated Android app keeps the request in the activity's saved state and the result, as the picked URI or the camera's file, in `ImagePickerModule.kt` until it's asked for, and reads it only then, handing it over only to a `recover` called within a minute of keeping it, and drops it when another picker or camera opens, even if the person cancels it, when the activity that received it finishes, and when an activity starts with no saved state. Without `recover`, as on iOS, `recover()` resolves with `null`.

## Open the share sheet

[`LynxShare`](/guide/share.md) calls a native module named `AngularLynxShare`. Register one with a `share(request, callback)` method:

- **`request`** holds `files`, and `title`, `text` and `url` when the app passes them. `files` is a list of `{ base64, name, type }`: each file's bytes as base64, its name and its MIME type, which may be empty.
- **`callback`** takes one map. Call it once per `share()`:
  - `{ shared: true }` once the person has shared
  - `{ shared: false }` when they closed the sheet
  - `{ error: "failed", message }`

Copy it from the generated apps:

- **iOS:** `ShareModule.swift` writes the files to temporary files and opens `UIActivityViewController`.
- **Android:** `ShareModule.kt` shares them through a `FileProvider`, given the activity as its param. The template declares the provider in `AndroidManifest.xml`, with `res/xml/share_paths.xml`.

For "Save Image" on iOS, your Info.plist needs `NSPhotoLibraryAddUsageDescription`. Without it, the iOS module leaves the action out.

Without the module, `share()` rejects with `unavailable`.

## Related

- [Embed in an existing native app](/guide/embedding/index.md)
- [Back button, memory and splash screen](/guide/embedding/shell-module.md)
- [Pass data from your native app](/guide/embedding/host-data.md)
- [`LynxNativeModule`](/api/angular-lynx/host/native-module.md)
