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

Swift API

The types you use to load bundles and show BakeNative screens on iOS and macOS.

ModulePlatformImport
BakeNativeKitiOS 16+import BakeNativeKit
BakeNativeAppKitmacOS 14+import BakeNativeAppKit

Both run the Rust core and re-export the shared Apple host layer (BakeNativeBundle, HydrationStrategy, the native module runtime the generated module contracts use). import BakeNativeCore still compiles: it is a compatibility module that re-exports the same layer.

The public API is only what an app calls or implements; native/swift-public-api.txt in the repository lists every public symbol. On both hosts BakeNativeView is the platform view (UIView, NSView) and BakeNativeSurface the SwiftUI view.

Preview API

These names reflect the current API and may still change before a first release.

BakeNativeBundle

A native bundle produced by bake build.

BakeNativeBundle(directory: url)                        // reads app.hbc, app.baked?, app.css?, manifest.json
BakeNativeBundle(hbc: hbcURL, frame: frameURL, css: cssURL)  // explicit files, no manifest check
MemberDescription
hbc, frame, cssFile URLs (frame and css are optional)
hostProtocolFrom the manifest (1 when absent)
incompatibility: String?Why this app cannot run the bundle, or nil
isSupported: Boolincompatibility == nil
static supportedHostProtocolHighest host protocol this version of BakeNative understands
static prewarm(_:) -> BakeNativePrewarmCancelPrepare a screen before it opens (iOS guide)

Native modules

Every view takes its native modules as one registry, modules:. The generated BakeNativeBundleModules.root(...) returns it: the modules the generated code creates (first-party and registry modules, a fresh instance per root) plus the app's own modules for that screen, one parameter each. A module the bundles call unguarded is a required parameter.

BakeNativeView(bundle: bundle, modules: BakeNativeBundleModules.root(orderScreen: OrderScreenModule(screen: self)))

Under the registry, every view adds the built-ins Platform, Storage and Haptics. See Native modules for specs, BakeNativeModuleRegistry and BakeNativeModuleError.

iOS views

BakeNativeViewController

BakeNativeViewController(bundle: BakeNativeBundle, hydration: HydrationStrategy = .load,
                      options: BakeNativeView.Options = .init(), props: [String: Any] = [:],
                      modules: BakeNativeModuleRegistry? = nil, views: [any BakeNativeNativeViewType.Type] = [])

A view controller with one BakeNativeView (bakeNativeView) pinned to the safe area. It applies the page's header and tab bar, launches in viewDidLoad and stops the runtime when popped or dismissed.

BakeNativeView (UIView)

BakeNativeView(bundle: BakeNativeBundle, hydration: HydrationStrategy = .load, options: Options = Options(),
            props: [String: Any] = [:], modules: BakeNativeModuleRegistry? = nil,
            views: [any BakeNativeNativeViewType.Type] = [])
MemberDescription
launch()Start. Waits until the view has a width
stop()Stop JavaScript and clear the views. Call when the screen is gone for good
reload()Launch the same bundle and props again
sendEvent(_:json:)A host event for the page, received with host.on(name); the payload is JSON text
onContentHeight, contentHeightThe page's height (with fitsContent)
onCanvasBackground, canvasBackgroundThe page's background color
scrollViewThe scroll view that hosts your page

BakeNativeView.Options

FieldDefaultDescription
edgeToEdgefalseThe page fills the whole view, under the status bar and the home indicator, and reads the insets from env(safe-area-inset-*)
fitsContentfalseNo scrolling of its own: the view is as tall as its page (a section of a native screen)

BakeNativeSurface (SwiftUI)

BakeNativeSurface(bundle: BakeNativeBundle, hydration: HydrationStrategy = .load,
               options: BakeNativeView.Options = .init(), props: [String: Any] = [:],
               modules: BakeNativeModuleRegistry? = nil, views: [any BakeNativeNativeViewType.Type] = [],
               onCreate: ((BakeNativeView) -> Void)? = nil)

onCreate runs once with the BakeNativeView, before launch: keep it to send host events.

macOS views

BakeNativeView (AppKit)

BakeNativeView(hydration: HydrationStrategy = .load, modules: BakeNativeModuleRegistry? = nil,
            views: [any BakeNativeNativeViewType.Type] = [])
host.launch(bundle, props: [String: Any] = [:])

An NSScrollView that hosts your page. stop(), reload() and sendEvent(_:json:) work as on iOS.

BakeNativeSurface (SwiftUI)

BakeNativeSurface(bundle: BakeNativeBundle?, hydration: HydrationStrategy = .load,
               props: [String: Any] = [:], modules: BakeNativeModuleRegistry? = nil,
               views: [any BakeNativeNativeViewType.Type] = [], onCreate: ((BakeNativeView) -> Void)? = nil)

onCreate runs once with the BakeNativeView, before launch.

HydrationStrategy

CaseTakes over the screen
.loadRight after the snapshot is drawn (default)
.idleWhen the main run loop is next idle
.interactionOn the first input on the snapshot; that input is delivered afterwards
.after(milliseconds:)After a fixed delay
.neverNot on its own (measurement hosts start it themselves)

JS → native goes through a native module of the screen; native → JS is a host event (sendEvent(_:json:)).

On this page