Pass data from your native app
Your native app can hand values to Angular. InitData goes to one LynxView when the native app opens your bundle. GlobalData is shared by every LynxView, and can change while the app runs. Data processors reshape InitData before Angular sees it.
This page involves native code, like the rest of Embedding.
Read InitData
initData()is a signal holding the whole object.- Give each field a fallback.
initData()is{}when no native app sent any, such as in Lynx Explorer or the web preview. - No setup.
LynxInitDatais provided in the root injector.
Read GlobalData
globalData() is an empty object when the native app sends no GlobalData. For the theme and the language, use the services in Theme and language instead.
Send the data from native code
Your native app sends InitData when it loads the bundle into a LynxView, and GlobalData whenever it likes.
On iOS, in Swift:
On Android, in Kotlin:
- Lynx calls GlobalData "global props", hence
updateGlobalProps. - Updates show up right away.
updateDatamerges the fields you send into the current InitData, andinitData()changes to match.updateGlobalPropsdoes the same forglobalData(), and for the theme and language services. - Set GlobalData before loading the bundle, so it's there when your app starts.
- Newer Lynx APIs load bundles with a
LynxLoadMetaobject instead. Lynx's Using Data from Host Platform guide shows them.
Reshape InitData with data processors
A data processor is a function that turns the data the native app sends into the shape your app wants. Register processors in main.ts, before bootstrapApplication():
- Register before bootstrapping, so the first data that arrives is already processed.
- Named processors run when native code names one in an update:
updateData(with: data, processorName: "refreshFeed")in Swift, orupdateData(data, "refreshFeed")in Kotlin. - A processor that throws gives
{}instead of crashing the app.
Fake the data while developing
While you develop in Lynx Explorer or the web preview, no native app sends your app its data. Fill in sample values yourself in main.ts, before bootstrapApplication():
- Fake InitData in a data processor. Lynx hands your app its InitData as the page starts, which replaces anything you set on
lynx. The processor runs on that data first. - Set GlobalData before bootstrapping, because
LynxGlobalDatareads it when it's created. isDevMode()isfalsein production builds, so the fake values never ship.- The spread keeps real values, if a native app does send some.
The data-flow example sets fake values the same way, in its src/main.ts.
What the generated apps pass
The apps ng build --platform makes are host apps too. They pass:
To pass anything else, eject or embed the app in your own.
Theme and language
Two GlobalData values have services of their own. Use them instead of reading globalData():
LynxThemereadstheme,'Dark'or'Light', and defaults to'Light'. See Dark mode.LynxLocalereadsappLocale. It falls back to thesourceLocaleinangular.json, then to'en-US'. See Internationalization.
They apply those defaults and check the values for you. Have your native app send theme and appLocale under exactly those names.
Type the data
By default, initData() and globalData() are typed as Record<string, unknown>. Describe your fields with module augmentation:
Now initData().userId is a string, with no casting.
Related
- Embed in an existing native app
- Host data API:
LynxInitData,LynxGlobalDataandregisterDataProcessors(). - Dark mode and Internationalization
- Using Data from Host Platform on lynxjs.org
