Skip to Content
Octapus

Embed in an iOS app

Add an Octapus screen to an existing UIKit or SwiftUI app.

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

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

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

Pick an entry point

import OctapusKit

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

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

Options, props and modules

let options = OctapusView.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
  storageScope: "default"   // namespace for the Storage module
)
let screen = OctapusViewController(bundle: bundle, hydration: .load, options: options,
                                   props: ["user": "ada"], modules: [ClipboardModule()])

props reach your root component on its first render; modules adds your own native modules.

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".

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: OctapusView records the first render of the first launch in its own cache (cachesFrames) 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. Follow it with onFrameCache:

OctapusSurface(bundle: bundle, onFrameCache: { event in
  switch event {
  case .hit(let url, let keyMs): print("cached first screen", url.lastPathComponent, keyMs)
  case .miss: print("no cached first screen yet")
  case .stored(_, let bytes, _): print("cached \(bytes) bytes")
  case .failed(let reason): print("cache failed:", reason)
  }
})

Without --bake, the first launch renders normally and later launches use this cache.

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