Skip to content
Preview: packages are not published yet.

Quickstart

Show a React screen inside your native app. The React team gives you one artifact per platform. You add one dependency and call generated Swift or Kotlin functions. You never touch JavaScript, C++ or a bundler.

A React screen delivered this way is called a surface. In this guide the artifact is named ShopReact and it has a surface named PointsHistory. Replace both with the names of your own artifact.

If nobody has made an artifact for you yet, this is the full path from an empty React project to a surface on screen in your app. The React steps are covered in Your first surface; this page covers the native steps.

  1. React project. Create a Nitro UI project in TypeScript, add nitro-ui-brownfield, nitro-ui-brownfield-cli and typescript, and add the brownfield key with name, version and ios.minimum. Apply the patches (Install, step 6). See Install and Your first surface.

  2. One surface. Write a component and register it with registerSurface. See Your first surface.

  3. Package it. In the React project run:

    Terminal window
    bun run nuib package --platform ios

    It writes a Swift package to <project>/.nuib/out/ios/ShopReact. The first run builds the engine and takes a few minutes; later runs reuse the cache in <project>/.nuib/cache. Add --ios-slices simulator to build only the simulator slice while you try things.

  4. Add the package to your app as a local package (step 1 below), start the runtime (step 2) and open the surface (step 3).

  5. Run on an Apple silicon simulator or a device (step 5).

For day-to-day work the React team does not repackage for every change: bun run nuib dev ios or bun run nuib dev android opens your app with the React project live in it. See Run and develop.

If the React team gives you the artifact, ask them for these things. You need them in the steps below.

You need iOS Android
Where the artifact is The Swift package directory: <project>/.nuib/out/ios/ShopReact as nuib package writes it, or a published version in the registry, <registry>/ShopReact/<version>/swiftpm The Maven repository: <project>/.nuib/out/android/maven as nuib package writes it, or the registry’s <registry>/maven; and the coordinate <group>:<artifact>:<version>
The name to import The module ShopReact The package <group>.<name in lower case>, for example com.example.shopreact
The surfaces The name of each surface and its props. Each surface is a generated type, so your editor’s autocomplete shows them too. The name of each surface and its props. Each surface is a generated type, so your editor’s autocomplete shows them too.

Your project also needs:

  • iOS: an Xcode project that can add Swift packages. The artifact sets a minimum iOS version, 16.0 or later. Your app must target at least that version.
  • Android: a Gradle project that uses AndroidX fragments, and an arm64-v8a device or emulator. The artifact sets a minSdk of 26 or higher.

Swift Package Manager. Add the artifact’s directory as a local package. In Xcode choose Add Package Dependencies, then Add Local, and select the directory (.nuib/out/ios/ShopReact, or swiftpm in a published version). Then link the product ShopReact to your app target.

If you declare dependencies in code:

Package.swift
dependencies: [
.package(path: "<React project>/.nuib/out/ios/ShopReact"),
]

That is all. Xcode embeds the framework itself. You add no C++ setting, no search path and no script phase.

The artifact has arm64 slices only: no x86_64 simulator slice. When you build for a generic simulator destination, for example in CI, add ARCHS=arm64 to xcodebuild; otherwise the build fails with Unable to resolve module dependency: 'ShopReact'.

The runtime is the part of the artifact that runs the React code. One runtime serves every surface in your app. Tell it to start when your app launches.

import ShopReact
// In your app delegate:
// application(_:didFinishLaunchingWithOptions:)
ShopReact.start(policy: .lazy)

The policy decides when the runtime boots:

  • .lazy / BootPolicy.LAZY (the default): nothing runs until the first surface opens. Your app launch costs nothing. The first surface pays for the boot.
  • .eager / BootPolicy.EAGER: the runtime boots inside start. Launch takes longer and the first surface opens faster.

Call start once. A second call replaces the configuration and reboots the runtime, and every open surface mounts again.

Each surface is a generated type. Its props are typed parameters: optional props have default values, a fixed set of strings is an enum, and numbers are Double. If you pass the wrong prop, your app does not compile.

A full-screen surface is a normal UIViewController on iOS and a normal Fragment on Android. You present it with your own navigation, so the back gesture and the back stack stay yours.

import ShopReact
let screen = PointsHistory.viewController(
userId: "42",
filter: .lastMonth,
compact: false)
navigationController?
.pushViewController(screen, animated: true)

Here userId is required, and filter and compact are optional: this is the surface written in Your first surface. Your surface has its own props.

A surface can also be a view inside one of your native screens, for example a card in a stack or a row in a list. Use view instead of viewController or fragment.

let block = PointsHistory.view(
userId: "42", compact: true)
stackView.addArrangedSubview(block)
block.heightAnchor
.constraint(equalToConstant: 240)
.isActive = true
  • You set the size. The view does not report the size of its React content. Give it a width and a height with your constraints or layout params (layoutParams above is yours), as you would for any view with no intrinsic size.
  • It opens by itself. The surface starts to load the first time the view enters a window. You do not call anything.
  • On Android, give the view an id. The view saves its surface in its view state, and Android only saves the state of views that have an id.

Build and run your app, then go to the screen that opens the surface. You should see the React screen fill the view controller or fragment, or fill the frame you gave the embedded view. With the lazy policy, the first surface takes a little longer than the next ones because it also boots the runtime.

To check what happens, watch the state of the surface. It moves from mounting to live when the first React render is on screen.

described mounting live

extension HomeViewController: SurfaceHostDelegate {
func surfaceHost(_ host: any SurfaceHost, didChange state: SurfaceState) {
print("\(host.descriptor.name) is \(state)")
}
func surfaceHost(_ host: any SurfaceHost, didReport timings: SurfaceTimings) {}
}
// before you push the screen
screen.delegate = self

A working surface logs mounting and then live. The library can also log one line per boot stage, in the form [brownfield] stage=<name> ms=<value>. On iOS the release artifact logs only when you turn logging on (see Turn on the logs), whatever the build configuration of your app. On Android a debuggable build of your app logs; the log tag is brownfield.

A surface that cannot load does not crash your app. It moves to the failed state and, unless you gave it a fallback view, its container stays empty. So an empty screen usually means a failure you are not observing yet. Add the delegate or listener from step 5 first, then find your case here.

What you see Why What to do
The state stays mounting and never changes start has not run. A surface opened before start waits for it. Call ShopReact.start at app launch (step 2). On Android, the log says that the surface is waiting for BrownfieldRuntime.configure.
Empty container, state is failed The surface failed and has no fallback view. Log failure.cause and failure.message. The causes are explained in Surfaces in your app, under “Failure causes”.
Embedded view is invisible, state is live The view has no size. Give it a width and a height (step 4).
iOS: the delegate is never called The host keeps its delegate weakly, and yours was released. Keep the delegate alive, for example use the presenting view controller.
Android: UnsatisfiedLinkError at launch or at the first surface The ABI filter is missing. Add abiFilters += "arm64-v8a" (step 1).
Android: IllegalStateException that says to use the runtime and surface hosts on the main thread You called the API from a background thread. Call start and every host method on the main thread.
Android: Gradle cannot resolve the dependency The Maven repository is not declared. Add the artifact’s Maven repository to settings.gradle.kts (step 1).
iOS: Unable to resolve module dependency: 'ShopReact' when building for a simulator A generic simulator destination also builds x86_64, and the artifact has no x86_64 slice. Add ARCHS=arm64 to xcodebuild, or build for one Apple silicon simulator.
iOS: no [brownfield] lines at all The release artifact logs only when logging is turned on, even in a Debug build of your app. Launch with -BrownfieldLogging YES (below).
  • iOS: the release artifact, the one your app normally depends on, is silent until you enable logging, in Debug and Release builds of your app alike. The dev variant that nuib dev and nuib link --dev install always logs.
  • Android: a debuggable build of your app logs. A release build is silent until you enable logging.

Launch argument:

-BrownfieldLogging YES