1--- 2title: Native Modules 3--- 4 5import { CodeBlocksTable } from '~/components/plugins/CodeBlocksTable'; 6import { PlatformTag } from '~/ui/components/Tag'; 7import { APIBox } from '~/components/plugins/APIBox'; 8 9> **warning** Expo Modules APIs are in beta and subject to breaking changes. 10 11The native modules API is an abstraction layer on top of [JSI](https://reactnative.dev/architecture/glossary#javascript-interfaces-jsi) and other low-level primities that React Native is built upon. It is built with modern languages (Swift and Kotlin) and provides an easy to use and convenient API that is consistent across platforms where possible. 12 13## Definition Components 14 15As you might have noticed in the snippets on the [Get Started](./get-started.mdx) page, each module class must implement the `definition` function. 16The module definition consists of the DSL components that describe the module's functionality and behavior. 17 18<APIBox header="Name"> 19 20Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument. Can be inferred from module's class name, but it's recommended to set it explicitly for clarity. 21 22```swift Swift / Kotlin 23Name("MyModuleName") 24``` 25 26</APIBox> 27<APIBox header="Constants"> 28 29Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary. 30 31<CodeBlocksTable> 32 33```swift 34// Created from the dictionary 35Constants([ 36 "PI": Double.pi 37]) 38 39// or returned by the closure 40Constants { 41 return [ 42 "PI": Double.pi 43 ] 44} 45``` 46 47```kotlin 48// Passed as arguments 49Constants( 50 "PI" to kotlin.math.PI 51) 52 53// or returned by the closure 54Constants { 55 return@Constants mapOf( 56 "PI" to kotlin.math.PI 57 ) 58} 59``` 60 61</CodeBlocksTable> 62</APIBox> 63<APIBox header="Function"> 64 65Defines a native synchronous function that will be exported to JavaScript. Synchronous means that when the function is executed in JavaScript, its native code is run on the same thread and blocks further execution of the script until the native function returns. 66 67#### Arguments 68 69- **name**: `String` — Name of the function that you'll call from JavaScript. 70- **body**: `(args...) -> ReturnType` — The closure to run when the function is called. 71 72The function can receive up to 8 arguments. This is due to the limitations of generics in both Swift and Kotlin, because this component must be implemented separately for each arity. 73 74See the [Argument Types](#argument-types) section for more details on what types can be used in the function body. 75 76<CodeBlocksTable> 77 78```swift 79Function("syncFunction") { (message: String) in 80 return message 81} 82``` 83 84```kotlin 85Function("syncFunction") { message: String -> 86 return@Function message 87} 88``` 89 90</CodeBlocksTable> 91 92```js JavaScript 93import { requireNativeModule } from 'expo-modules-core'; 94 95// Assume that we have named the module "MyModule" 96const MyModule = requireNativeModule('MyModule'); 97 98function getMessage() { 99 return MyModule.syncFunction('bar'); 100} 101``` 102 103</APIBox> 104<APIBox header="AsyncFunction"> 105 106Defines a JavaScript function that always returns a `Promise` and whose native code is by default dispatched on the different thread than the JavaScript runtime runs on. 107 108#### Arguments 109 110- **name**: `String` — Name of the function that you'll call from JavaScript. 111- **body**: `(args...) -> ReturnType` — The closure to run when the function is called. 112 113If the type of the last argument is `Promise`, the function will wait for the promise to be resolved or rejected before the response is passed back to JavaScript. Otherwise, the function is immediately resolved with the returned value or rejected if it throws an exception. 114The function can receive up to 8 arguments (including the promise). 115 116See the [Argument Types](#argument-types) section for more details on what types can be used in the function body. 117 118It is recommended to use `AsyncFunction` over `Function` when it: 119 120- does I/O bound tasks such as sending network requests or interacting with the file system 121- needs to be run on different thread, e.g. the main UI thread for UI-related tasks 122- is an extensive or long-lasting operation that would block the JavaScript thread which in turn would reduce the responsiveness of the application 123 124<CodeBlocksTable> 125 126```swift 127AsyncFunction("asyncFunction") { (message: String, promise: Promise) in 128 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { 129 promise.resolve(message) 130 } 131} 132``` 133 134```kotlin 135AsyncFunction("asyncFunction") { message: String, promise: Promise -> 136 launch(Dispatchers.Main) { 137 promise.resolve(message) 138 } 139} 140``` 141 142</CodeBlocksTable> 143 144```js JavaScript 145import { requireNativeModule } from 'expo-modules-core'; 146 147// Assume that we have named the module "MyModule" 148const MyModule = requireNativeModule('MyModule'); 149 150async function getMessageAsync() { 151 return await MyModule.asyncFunction('bar'); 152} 153``` 154 155</APIBox> 156<APIBox header="Events"> 157 158Defines event names that the module can send to JavaScript. 159 160<CodeBlocksTable> 161 162```swift 163Events("onCameraReady", "onPictureSaved", "onBarCodeScanned") 164``` 165 166```kotlin 167Events("onCameraReady", "onPictureSaved", "onBarCodeScanned") 168``` 169 170</CodeBlocksTable> 171 172See [Sending events](#sending-events) to learn how to send events from the native code to JavaScript/TypeScript. 173 174</APIBox> 175<APIBox header="ViewManager"> 176 177> **warning** **Deprecated**: To better integrate with [React Native's new architecture (Fabric)](https://reactnative.dev/architecture/fabric-renderer) and its recycling mechanism, as of SDK 47 the `ViewManager` component is deprecated in favor of [`View`](#view) with a view class passed as the first argument. This component will be removed in SDK 48. 178 179Enables the module to be used as a view manager. The view manager definition is built from the definition components used in the closure passed to `ViewManager`. Definition components that are accepted as part of the view manager definition: [`View`](#view), [`Prop`](#prop). 180 181<CodeBlocksTable> 182 183```swift 184ViewManager { 185 View { 186 MyNativeView() 187 } 188 189 Prop("isHidden") { (view: UIView, hidden: Bool) in 190 view.isHidden = hidden 191 } 192} 193``` 194 195```kotlin 196ViewManager { 197 View { context -> 198 MyNativeView(context) 199 } 200 201 Prop("isHidden") { view: View, hidden: Bool -> 202 view.isVisible = !hidden 203 } 204} 205``` 206 207</CodeBlocksTable> 208</APIBox> 209<APIBox header="View"> 210 211Enables the module to be used as a native view. Definition components that are accepted as part of the view definition: [`Prop`](#prop), [`Events`](#events). 212 213#### Arguments 214 215- **viewType** — The class of the native view that will be rendered. 216- **definition**: `() -> ViewDefinition` — A builder of the view definition. 217 218<CodeBlocksTable> 219 220```swift 221View(UITextView.self) { 222 Prop("text") { ... } 223} 224``` 225 226```kotlin 227View(TextView::class) { 228 Prop("text") { ... } 229} 230``` 231 232</CodeBlocksTable> 233 234> Support for rendering SwiftUI views is planned. For now, you can use [`UIHostingController`](https://developer.apple.com/documentation/swiftui/uihostingcontroller) and add its content view to your UIKit view. 235 236</APIBox> 237<APIBox header="Prop"> 238 239Defines a setter for the view prop of given name. 240 241#### Arguments 242 243- **name**: `String` — Name of view prop that you want to define a setter. 244- **setter**: `(view: ViewType, value: ValueType) -> ()` — Closure that is invoked when the view rerenders. 245 246This property can only be used within a [`ViewManager`](#viewmanager) closure. 247 248<CodeBlocksTable> 249 250```swift 251Prop("background") { (view: UIView, color: UIColor) in 252 view.backgroundColor = color 253} 254``` 255 256```kotlin 257Prop("background") { view: View, @ColorInt color: Int -> 258 view.setBackgroundColor(color) 259} 260``` 261 262</CodeBlocksTable> 263 264> **Note** Props of function type (callbacks) are not supported yet. 265 266</APIBox> 267<APIBox header="OnCreate"> 268 269Defines module's lifecycle listener that is called right after module initialization. If you need to set up something when the module gets initialized, use this instead of module's class initializer. 270 271</APIBox> 272<APIBox header="OnDestroy"> 273 274Defines module's lifecycle listener that is called when the module is about to be deallocated. Use it instead of module's class destructor. 275 276</APIBox> 277<APIBox header="OnStartObserving"> 278 279Defines the function that is invoked when the first event listener is added. 280 281</APIBox> 282<APIBox header="OnStopObserving"> 283 284Defines the function that is invoked when all event listeners are removed. 285 286</APIBox> 287<APIBox header="OnAppContextDestroys"> 288 289Defines module's lifecycle listener that is called when the app context owning the module is about to be deallocated. 290 291</APIBox> 292<APIBox header="OnAppEntersForeground" platforms={["ios"]}> 293 294Defines the listener that is called when the app is about to enter the foreground mode. 295 296> **Note** This function is not available on Android — you may want to use [`OnActivityEntersForeground`](#onactivityentersforeground) instead. 297 298</APIBox> 299<APIBox header="OnAppEntersBackground" platforms={["ios"]}> 300 301Defines the listener that is called when the app enters the background mode. 302 303> **Note** This function is not available on Android — you may want to use [`OnActivityEntersBackground`](#onactivityentersbackground) instead. 304 305</APIBox> 306<APIBox header="OnAppBecomesActive" platforms={["ios"]}> 307 308Defines the listener that is called when the app becomes active again (after `OnAppEntersForeground`). 309 310> **Note** This function is not available on Android — you may want to use [`OnActivityEntersForeground`](#onactivityentersforeground) instead. 311 312</APIBox> 313<APIBox header="OnActivityEntersForeground" platforms={["android"]}> 314 315Defines the activity lifecycle listener that is called right after the activity is resumed. 316 317> **Note** This function is not available on iOS — you may want to use [`OnAppEntersForeground`](#onappentersforeground) instead. 318 319</APIBox> 320<APIBox header="OnActivityEntersBackground" platforms={["android"]}> 321 322Defines the activity lifecycle listener that is called right after the activity is paused. 323 324> **Note** This function is not available on iOS — you may want to use [`OnAppEntersBackground`](#onappentersbackground) instead. 325 326</APIBox> 327<APIBox header="OnActivityDestroys" platforms={["android"]}> 328 329Defines the activity lifecycle listener that is called when the activity owning the JavaScript context is about to be destroyed. 330 331> **Note** This function is not available on iOS — you may want to use [`OnAppEntersBackground`](#onappentersbackground) instead. 332 333</APIBox> 334 335## Argument Types 336 337Fundamentally, only primitive and serializable data can be passed back and forth between the runtimes. However, usually native modules need to receive custom data structures — more sophisticated than just the dictionary/map where the values are of unknown (`Any`) type and so each value has to be validated and casted on its own. The Expo Modules API provides protocols to make it more convenient to work with data objects, to provide automatic validation, and finally, to ensure native type-safety on each object member. 338 339<APIBox header="Primitives"> 340 341All functions and view prop setters accept all common primitive types in Swift and Kotlin as the arguments. This includes arrays, dictionaries/maps and optionals of these primitive types. 342 343| Language | Supported primitive types | 344| -------- | ------------------------------------------------------------------------------------------------------------------------------ | 345| Swift | `Bool`, `Int`, `Int8`, `Int16`, `Int32`, `Int64`, `UInt`, `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Float32`, `Double`, `String` | 346| Kotlin | `Boolean`, `Int`, `UInt`, `Float`, `Double`, `String`, `Pair` | 347 348</APIBox> 349<APIBox header="Convertibles"> 350 351_Convertibles_ are native types that can be initialized from certain specific kinds of data received from JavaScript. Such types are allowed to be used as an argument type in `Function`'s body. For example, when the `CGPoint` type is used as a function argument type, its instance can be created from an array of two numbers `(x, y)` or a JavaScript object with numeric `x` and `y` properties. 352 353Some common iOS types from `CoreGraphics` and `UIKit` system frameworks are already made convertible. 354 355| Native iOS Type | TypeScript | 356| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 357| `URL` | `string` with a URL. When scheme is not provided, it's assumed to be a file URL. | 358| `CGFloat` | `number` | 359| `CGPoint` | `{ x: number, y: number }` or `number[]` with _x_ and _y_ coords | 360| `CGSize` | `{ width: number, height: number }` or `number[]` with _width_ and _height_ | 361| `CGVector` | `{ dx: number, dy: number }` or `number[]` with _dx_ and _dy_ vector differentials | 362| `CGRect` | `{ x: number, y: number, width: number, height: number }` or `number[]` with _x_, _y_, _width_ and _height_ values | 363| `CGColor`<br/>`UIColor` | Color hex strings (`#RRGGBB`, `#RRGGBBAA`, `#RGB`, `#RGBA`), named colors following the [CSS3/SVG specification](https://www.w3.org/TR/css-color-3/#svg-color) or `"transparent"` | 364 365Similarly, some common Android types from packages like `java.io`, `java.net`, or `android.graphics` are also made convertible. 366 367| Native Android Type | TypeScript | 368| --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 369| `java.net.URL` | `string` with a URL. Note that the scheme has to be provided | 370| `android.net.Uri`<br/>`java.net.URI` | `string` with a URI. Note that the scheme has to be provided | 371| `java.io.File`<br/>`java.nio.file.Path` | `string` with a path to the file | 372| `android.graphics.Color` | Color hex strings (`#RRGGBB`, `#RRGGBBAA`, `#RGB`, `#RGBA`), named colors following the [CSS3/SVG specification](https://www.w3.org/TR/css-color-3/#svg-color) or `"transparent"` | 373| `kotlin.Pair<A, B>` | Array with two values, where the first one is of type _A_ and the second is of type _B_ | 374 375</APIBox> 376<APIBox header="Records"> 377 378_Record_ is a convertible type and an equivalent of the dictionary (Swift) or map (Kotlin), but represented as a struct where each field can have its own type and provide a default value. 379It is a better way to represent a JavaScript object with the native type-safety. 380 381<CodeBlocksTable> 382 383```swift 384struct FileReadOptions: Record { 385 @Field 386 var encoding: String = "utf8" 387 388 @Field 389 var position: Int = 0 390 391 @Field 392 var length: Int? 393} 394 395// Now this record can be used as an argument of the functions or the view prop setters. 396Function("readFile") { (path: String, options: FileReadOptions) -> String in 397 // Read the file using given `options` 398} 399``` 400 401```kotlin 402class FileReadOptions : Record { 403 @Field 404 val encoding: String = "utf8" 405 406 @Field 407 val position: Int = 0 408 409 @Field 410 val length: Int? 411} 412 413// Now this record can be used as an argument of the functions or the view prop setters. 414Function("readFile") { path: String, options: FileReadOptions -> 415 // Read the file using given `options` 416} 417``` 418 419</CodeBlocksTable> 420</APIBox> 421<APIBox header="Enums"> 422 423With enums we can go even further with the above example (with `FileReadOptions` record) and limit supported encodings to `"utf8"` and `"base64"`. To use an enum as an argument or record field, it must represent a primitive value (e.g. `String`, `Int`) and conform to `Enumerable`. 424 425<CodeBlocksTable> 426 427```swift 428enum FileEncoding: String, Enumerable { 429 case utf8 430 case base64 431} 432 433struct FileReadOptions: Record { 434 @Field 435 var encoding: FileEncoding = .utf8 436 // ... 437} 438``` 439 440```kotlin 441// Note: the constructor must have an argument called value. 442enum class FileEncoding(val value: String) : Enumerable { 443 utf8("utf8"), 444 base64("base64") 445} 446 447class FileReadOptions : Record { 448 @Field 449 val encoding: FileEncoding = FileEncoding.utf8 450 // ... 451} 452``` 453 454</CodeBlocksTable> 455</APIBox> 456<APIBox header="Eithers"> 457 458There are some use cases where you want to pass various types for a single function argument. This is where Either types might come in handy. 459They act as a container for a value of one of a couple of types. 460 461<CodeBlocksTable> 462 463```swift 464Function("foo") { (bar: Either<String, Int>) in 465 if let bar: String = bar.get() { 466 // `bar` is a String 467 } 468 if let bar: Int = bar.get() { 469 // `bar` is an Int 470 } 471} 472``` 473 474```kotlin 475Function("foo") { bar: Either<String, Int> -> 476 bar.get(String::class).let { 477 // `it` is a String 478 } 479 bar.get(Int::class).let { 480 // `it` is an Int 481 } 482} 483``` 484 485</CodeBlocksTable> 486 487The implementation for three Either types is currently provided out of the box, allowing you to use up to four different subtypes. 488 489- `Either<FirstType, SecondType>` — A container for one of two types. 490- `EitherOfThree<FirstType, SecondType, ThirdType>` — A container for one of three types. 491- `EitherOfFour<FirstType, SecondType, ThirdType, FourthType>` — A container for one of four types. 492 493> Either types are available as of SDK 47. 494 495</APIBox> 496 497## Guides 498 499<APIBox header="Sending events"> 500 501While JavaScript/TypeScript to Native communication is mostly covered by native functions, you might also want to let the JavaScript/TypeScript code know about certain system events, for example, when the clipboard content changes. 502 503To do this, in the module definition, you need to provide the event names that the module can send using the [Events](#events) definition component. After that, you can use the `sendEvent(eventName, payload)` function on the module instance to send the actual event with some payload. For example, a minimal clipboard implementation that sends native events may look like this: 504 505<CodeBlocksTable> 506 507```swift 508let CLIPBOARD_CHANGED_EVENT_NAME = "onClipboardChanged" 509 510public class ClipboardModule: Module { 511 public func definition() -> ModuleDefinition { 512 Events(CLIPBOARD_CHANGED_EVENT_NAME) 513 514 OnStartObserving { 515 NotificationCenter.default.addObserver( 516 self, 517 selector: #selector(self.clipboardChangedListener), 518 name: UIPasteboard.changedNotification, 519 object: nil 520 ) 521 } 522 523 OnStopObserving { 524 NotificationCenter.default.removeObserver( 525 self, 526 name: UIPasteboard.changedNotification, 527 object: nil 528 ) 529 } 530 } 531 532 @objc 533 private func clipboardChangedListener() { 534 sendEvent(CLIPBOARD_CHANGED_EVENT_NAME, [ 535 "contentTypes": availableContentTypes() 536 ]) 537 } 538} 539``` 540 541```kotlin 542const val CLIPBOARD_CHANGED_EVENT_NAME = "onClipboardChanged" 543 544class ClipboardModule : Module() { 545 override fun definition() = ModuleDefinition { 546 Events(CLIPBOARD_CHANGED_EVENT_NAME) 547 548 OnStartObserving { 549 clipboardManager?.addPrimaryClipChangedListener(listener) 550 } 551 552 OnStopObserving { 553 clipboardManager?.removePrimaryClipChangedListener(listener) 554 } 555 } 556 557 private val clipboardManager: ClipboardManager? 558 get() = appContext.reactContext?.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager 559 560 private val listener = ClipboardManager.OnPrimaryClipChangedListener { 561 clipboardManager?.primaryClipDescription?.let { clip -> 562 [email protected]( 563 CLIPBOARD_CHANGED_EVENT_NAME, 564 bundleOf( 565 "contentTypes" to availableContentTypes(clip) 566 ) 567 ) 568 } 569 } 570} 571``` 572 573</CodeBlocksTable> 574 575To subscribe to these events in JavaScript/TypeScript, you need to wrap the native module with `EventEmitter` class as shown: 576 577```ts TypeScript 578import { requireNativeModule, EventEmitter, Subscription } from 'expo-modules-core'; 579 580const ClipboardModule = requireNativeModule('Clipboard'); 581const emitter = new EventEmitter(ClipboardModule); 582 583export function addClipboardListener(listener: (event) => void): Subscription { 584 return emitter.addListener('onClipboardChanged', listener); 585} 586``` 587 588</APIBox> 589 590## Examples 591 592<CodeBlocksTable> 593 594```swift 595public class MyModule: Module { 596 public func definition() -> ModuleDefinition { 597 Name("MyFirstExpoModule") 598 599 Function("hello") { (name: String) in 600 return "Hello \(name)!" 601 } 602 } 603} 604``` 605 606```kotlin 607class MyModule : Module() { 608 override fun definition() = ModuleDefinition { 609 Name("MyFirstExpoModule") 610 611 Function("hello") { name: String -> 612 return "Hello $name!" 613 } 614 } 615} 616``` 617 618</CodeBlocksTable> 619 620For more examples from real modules, you can refer to Expo modules that already use this API on GitHub: 621 622- `expo-battery` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-battery/ios)) 623- `expo-cellular` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-cellular/ios), [Kotlin](https://github.com/expo/expo/tree/main/packages/expo-cellular/android/src/main/java/expo/modules/cellular)) 624- `expo-clipboard` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-clipboard/ios), [Kotlin](https://github.com/expo/expo/tree/main/packages/expo-clipboard/android/src/main/java/expo/modules/clipboard)) 625- `expo-crypto` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-crypto/ios), [Kotlin](https://github.com/expo/expo/tree/main/packages/expo-crypto/android/src/main/java/expo/modules/crypto)) 626- `expo-haptics` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-haptics/ios)) 627- `expo-image-manipulator` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-image-manipulator/ios)) 628- `expo-image-picker` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-image-picker/ios), [Kotlin](https://github.com/expo/expo/tree/main/packages/expo-image-picker/android/src/main/java/expo/modules/imagepicker)) 629- `expo-linear-gradient` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-linear-gradient/ios), [Kotlin](https://github.com/expo/expo/tree/main/packages/expo-linear-gradient/android/src/main/java/expo/modules/lineargradient)) 630- `expo-localization` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-localization/ios), [Kotlin](https://github.com/expo/expo/tree/main/packages/expo-localization/android/src/main/java/expo/modules/localization)) 631- `expo-store-review` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-store-review/ios)) 632- `expo-system-ui` ([Swift](https://github.com/expo/expo/tree/main/packages/expo-system-ui/ios/ExpoSystemUI)) 633- `expo-web-browser` ([Swift](https://github.com/expo/expo/blob/main/packages/expo-web-browser/ios), [Kotlin](https://github.com/expo/expo/blob/main/packages/expo-web-browser/android/src/main/java/expo/modules/webbrowser)) 634