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 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.
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.
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 calls a native module named AngularLynxWebAuth. Register one with a start(request, callback) method:
requestholdsurl,callbackSchemeandephemeral.callbacktakes one map. Call it once perstart():{ url }: the whole URL the page redirected to oncallbackScheme{ error, message }, whereerroriscanceledorfailed
Copy it from the generated apps:
- iOS:
WebAuthModule.swiftusesASWebAuthenticationSession. - Android:
WebAuthModule.ktopens 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 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 underkey.setItem(key, value, callback),removeItem(key, callback)andclear(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.swiftusesUserDefaults.SecureStorageModule.swiftuses the Keychain. - Android:
LocalStorageModule.ktusesSharedPreferences.SecureStorageModule.ktencrypts 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 calls a native module named AngularLynxLinking. Register one with these methods:
openURL(url, callback)opensurlwhere the system opens it. It calls back with{}once it's handed over.openInApp(url, options, callback)shows anhttporhttpsurlin a browser sheet over the app. It calls back with{}once the person is back.canOpenURL(url, callback)calls back with{ value }:truewhen something on the device opensurl.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.swiftusesUIApplication.openandSFSafariViewController, and opensUIApplication.openSettingsURLStringfor Settings. - Android:
LinkingModule.ktsends anACTION_VIEWintent and opens a Custom Tab, and anACTION_APPLICATION_DETAILS_SETTINGSintent 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 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.) 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
- Keep the link until the page takes it.
- Send the global event
angularLynxURLto the LynxView, with no parameters. - Answer
takeURLs(callback). The page calls it on theAngularLynxLinkingmodule. 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'sconnectionOptions.urlContextsandscene(_:openURLContexts:)), withLinkingModule.swift'sLinkCenter - Android:
MainActivity.kt(the intent inonCreateandonNewIntent), withLinkingModule.kt
Without them, getInitialURL() is null and no link reaches the page.
Hear the app's shortcuts
LynxShortcuts 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:
- 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. - Sends the global event
angularLynxShortcutto the LynxView, with no parameters, each time it keeps one while the view exists. - Answers
take(callback)with{ shortcuts }, the ones kept since the last call, oldest first, and forgets them. The page callstakeon each event, and once as it starts.
Copy it from the generated apps:
- iOS:
ShortcutsModule.swift. The generated App Intents hand their shortcut to itsShortcutCenter. - Android:
ShortcutsModule.kt, whichMainActivityhands a shortcut's intent to.
Without the module, no shortcut ever reaches the page.
Let someone pick a photo
LynxImagePicker calls a native module named AngularLynxImagePicker. Register one with a pick(options, callback) method:
optionsholdsmaxDimensionandqualitywhen the app passes them. With either, re-encode the photo as a JPEG, upright and scaled down tomaxDimension.callbacktakes one map. Call it once perpick():{ 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.swiftusesPHPickerViewControllerand ImageIO. - Android:
ImagePickerModule.kthas 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 needsNSCameraUsageDescription. - Android:
ActivityResultContracts.TakePicture, after asking forCAMERAif 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 calls a native module named AngularLynxShare. Register one with a share(request, callback) method:
requestholdsfiles, andtitle,textandurlwhen the app passes them.filesis a list of{ base64, name, type }: each file's bytes as base64, its name and its MIME type, which may be empty.callbacktakes one map. Call it once pershare():{ 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.swiftwrites the files to temporary files and opensUIActivityViewController. - Android:
ShareModule.ktshares them through aFileProvider, given the activity as its param. The template declares the provider inAndroidManifest.xml, withres/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.
