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

# 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](/guide/native-plugins/index.md), 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](/guide/glossary.md#ui-thread) first.

### On iOS

A module is a Swift class that adopts `LynxModule`:

```swift title="native/greeter/ios/GreeterModule.swift"
import Lynx
import UIKit

final class GreeterModule: NSObject, LynxModule {
  static var name: String { "Greeter" }

  static var methodLookup: [String: String] {
    ["greet": NSStringFromSelector(#selector(greet(_:)))]
  }

  private let host: AngularLynxPluginHost?

  override init() {
    host = nil
    super.init()
  }

  // Swift can't import Lynx's optional initWithParam: as optional.
  init(param: Any) {
    host = param as? AngularLynxPluginHost
    super.init()
  }

  @objc func greet(_ name: String) -> String {
    "Hello, \(name), from iOS \(UIDevice.current.systemVersion)"
  }
}
```

- **`methodLookup`** maps each method's JavaScript name to its [selector](/guide/glossary.md#selector).
- **The shell creates it with an `AngularLynxPluginHost`** as its param. The host's `viewController` is the [view controller](/guide/glossary.md#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:

```kotlin title="native/greeter/android/com/example/greeter/GreeterModule.kt"
package com.example.greeter

import android.content.Context
import android.os.Build
import com.lynx.jsbridge.LynxMethod
import com.lynx.jsbridge.LynxModule

class GreeterModule(context: Context, param: Any?) : LynxModule(context, param) {
    @LynxMethod
    fun greet(name: String): String = "Hello, $name, from Android ${Build.VERSION.RELEASE}"
}
```

### Register it

Name the module and its class in the plugin's `modules`, for each platform:

```json title="angular.json"
"ios": { "modules": { "Greeter": "GreeterModule" } },
"android": {
  "packageName": "com.example.greeter",
  "modules": { "Greeter": "GreeterModule" }
}
```

See [Make a plugin](/guide/native-plugins/index.md#make-a-plugin) for the rest of the entry, and the names you can't use.

## Call it

Describe the module in [`NativeModuleMap`](/api/angular-lynx/host/native-module.md#nativemodulemap), and [`getNativeModule()`](/api/angular-lynx/host/native-module.md) types it:

```typescript title="src/app/greeter.ts"
export {};

declare module '@blotch/angular-lynx' {
  interface NativeModuleMap {
    Greeter: { greet(name: string): string };
  }
}
```

Import the file where you call the module, so it's in the compilation. From Angular's code, each call returns a promise:

```typescript
import './greeter';

// undefined where the host has no Greeter module: no module, or a call
// that rejects.
const greeting = await inject(LynxNativeModule)
  .getNativeModule('Greeter')
  ?.greet('Ada')
  .catch(() => undefined);
```

**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](/guide/native-plugins/index.md#elsewhere-your-plugin-isnt-there).

## Related

- [Add native code](/guide/native-plugins/index.md): make a plugin, build and run it
- [Write a native element](/guide/native-plugins/elements.md)
- [`LynxNativeModule`](/api/angular-lynx/host/native-module.md)
- The [Native Plugin example](/examples/native-plugin.md)
- Lynx's [Native Modules](https://lynxjs.org/guide/use-native-modules) guide
