BakeNative is under active development. APIs may change.
Skip to Content
BakeNative

Embed in an iOS app

Add a BakeNative screen to an existing UIKit or SwiftUI app.

BakeNative on iOS is built for apps that already exist. A BakeNative screen is one view or view controller you put wherever you want; the rest of your app stays native.

Add the BakeNativeKit product (Installation), then copy dist/ios from bake build --target ios --bake into your app target as a folder. import BakeNativeKit gives you everything below, BakeNativeBundle included.

To ship the screen as prebuilt binaries instead, so the app adds one Swift package and needs no bundle copying or BakeNative source, see Ship as a Swift package.

Pick an entry point

import BakeNativeKit

let url = Bundle.main.url(forResource: "ios", withExtension: nil)!
let screen = BakeNativeViewController(bundle: BakeNativeBundle(directory: url))
navigationController?.pushViewController(screen, animated: true)

Pins one BakeNative view to the safe area, extends your page background under the bars, and stops the runtime when the screen is popped or dismissed.

To draw the page itself under the status bar and the home indicator, open it edge to edge and pad with env(safe-area-inset-*) in CSS (see Safe area):

let screen = BakeNativeViewController(bundle: bundle, options: BakeNativeView.Options(edgeToEdge: true))

Options, props and modules

let options = BakeNativeView.Options(
  cachesFrames: true,       // keep a first screen on device for bundles without a snapshot (default)
  timeLimitMs: 0,           // stop runaway JS after N ms per call (0 = off)
  builtinModules: true,     // Platform, Storage, Haptics (Clipboard, Linking, Share: bake add)
  storageScope: "default"   // namespace for the Storage module
)
let screen = BakeNativeViewController(bundle: bundle, hydration: .load, options: options,
                                   props: ["user": "ada"], modules: BakeNativeBundleModules.root())

props reach your root component on its first render. Native modules come from the generated registration as one registry, modules: BakeNativeBundleModules.root(...): the modules the generated code creates plus this screen's own, one parameter each.

A section inside a native screen

In an existing app, a BakeNative page is often one part of a UIKit or SwiftUI screen: native rows above and below it, one scroll for the whole screen. Open it with fitsContent, so the view is as tall as its page and does not scroll on its own:

// UIKit (Auto Layout, a table cell, a stack view): the height is the view's intrinsicContentSize.
let section = BakeNativeView(bundle: bundle, options: BakeNativeView.Options(fitsContent: true),
                          modules: BakeNativeBundleModules.root(orderScreen: OrderScreenModule(screen: self)))
section.onContentHeight = { _ in self.tableView.performBatchUpdates(nil) }  // a self-sizing cell
section.launch()

// SwiftUI: BakeNativeSurface sizes itself (sizeThatFits) inside a ScrollView or List.
ScrollView {
  SummaryHeader()           // the app's own SwiftUI view
  BakeNativeSurface(bundle: bundle, options: .init(fitsContent: true),
                 modules: BakeNativeBundleModules.root(activityScreen: ActivityScreenModule(model: model)))
  NativeFooter()
}

The page talks to the screen around it in both directions:

  • JS → native: through a module declared in a spec (native/order-screen/OrderScreen.native.ts). The screen creates it with itself (init(screen:); the module holds it weakly) and passes it in BakeNativeBundleModules.root(...). native.OrderScreen.setTip(2) in JS. Sync members are called on the JS thread, so queue UIKit work on main (DispatchQueue.main.async) without waiting for it.
  • Native → JS: a host event, bakeNativeView.sendEvent("speed", json: json), received with host.on('speed', …).

If the bundle calls a module unguarded, it is a required parameter of BakeNativeBundleModules.root(...): a screen that does not pass it does not compile. A bundle shared by screens with different modules guards the per-screen ones with hasNative('OrderScreen'): they become optional parameters, and each screen runs without the others' modules.

In fitsContent mode the page is laid out at a 1 pt viewport height: vh units, position: fixed and virtual lists (which follow the visible part of their own scroll) do not apply.

hydration decides when JavaScript takes over the screen. See Choosing when to hydrate.

timeLimitMs only works if the bytecode was compiled to allow interruption: build with --hermesc-flags "-O -emit-async-break-check".

120 Hz on ProMotion iPhones

BakeNative draws at up to 120 Hz by default. On iPhone, a BakeNative screen is held at 60 Hz unless the app's Info.plist opts in (UIKit's own scrolling is not affected), and in an existing app that file is yours, so add one key:

<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>

Apps made with bake create already have it. Without it, a debug build logs this once on a ProMotion iPhone: [BakeNative] warning: ProMotion display, but CADisableMinimumFrameDurationOnPhone is missing: BakeNative is limited to 60 Hz. iPads and 60 Hz iPhones need nothing.

The first screen on iOS

With --bake, the bundle carries a first-screen snapshot that the device lays out at the view's real size with its own fonts, so the very first launch already shows the screen without waiting for JS. The laid-out result is cached on the device per size, color scheme, app build and OS, so later launches skip the layout too. If the device rotates or switches to dark mode while the snapshot is still showing, it is laid out again before your app takes over.

Bundles built without --bake get a first screen too: BakeNativeView records the first render of the first launch in its own cache and draws it on later launches. That cache is keyed by the bundle's bytecode and CSS, the view width and the color scheme, so a new bundle never shows a stale screen. Without --bake, the first launch renders normally and later launches use this cache.

Prepare a screen before it opens

let cancel = BakeNativeBundle.prewarm(bundle)   // at app launch, or when the button that opens the screen shows
cancel()                                     // only if the screen will not open after all
// several: let cancels = bundles.map { BakeNativeBundle.prewarm($0) }

prewarm prepares the bundle's first screen on a background thread, without running JavaScript, and warms what the first BakeNative screen of the app would otherwise load on the main thread (text, fonts, blur, the bundle's files). Open the screen as usual (BakeNativeViewController(bundle: bundle)): the view takes what was prepared. A memory warning or the app going to the background releases it by itself. You can ignore the result; cancel() releases only what that call prepared. A screen that does not open as a pushed screen (edge to edge, in a sheet) is prepared at its real size from its second prewarm on (in the same run, or from the frame it cached on disk); the first time, the view lays it out again when it opens.

Embedding cost

What one BakeNative screen adds to an existing app (time to the first frame and to interactive after a push, binary size, memory), measured against the same screen in UIKit and SwiftUI: see Embedding cost.

Light and dark

The snapshot stores your styles for both color schemes, and the device draws the one that matches the system. Cached first screens are kept per color scheme too, so switching to dark mode does not reuse a light screen.

On this page