Write a native module
A native module is a Swift or Kotlin class your Angular code calls like the runtime's own modules. Add it to a native plugin, then call it with getNativeModule().
Write the module
A module's methods take and return what JSON can hold, and functions for callbacks.
Lynx calls them on a thread of its own. So a method that touches the UI moves to the platform's UI thread first.
On iOS
A module is a Swift class that adopts LynxModule:
methodLookupmaps each method's JavaScript name to its selector.- The shell creates it with an
AngularLynxPluginHostas its param. The host'sviewControlleris the view controller showing the app. Use it for a module that presents a screen of its own. - The host holds the controller weakly. Lynx keeps a module's param as long as the view, which the controller keeps.
On Android
A module extends LynxModule and marks its methods @LynxMethod. The shell creates it with its activity as the param:
Register it
Name the module and its class in the plugin's modules, for each platform:
See Make a plugin for the rest of the entry, and the names you can't use.
Call it
Describe the module in NativeModuleMap, and getNativeModule() types it:
Import the file where you call the module, so it's in the compilation. From Angular's code, each call returns a promise:
Handle a missing module. Lynx Explorer and the web preview don't have your plugin. There, getNativeModule() gives undefined, or a module whose calls reject. The ?. and .catch() above cover both. See Elsewhere, your plugin isn't there.
Related
- Add native code: make a plugin, build and run it
- Write a native element
LynxNativeModule- The Native Plugin example
- Lynx's Native Modules guide
