SwiftBrowser docs
View as MarkdownEdit on GitHub

SwiftBrowser#

Run SwiftUI apps in the browser, so you can build and iterate on iOS apps without a Mac for most of the day.

An app's source imports SwiftUI and compiles unchanged for both iOS and the browser. On Apple platforms it uses Apple's SwiftUI. In the browser it compiles to WebAssembly against the API-compatible shim in Sources/SwiftUI, which turns the view tree into render ops that a small TypeScript renderer draws inside an iPhone frame, or full screen on a phone.

npx @swiftbrowser/cli doctor --install          # the wasm SDK and wasm-opt (Swift itself is optional)
npx @swiftbrowser/cli init MyApp                # a new SwiftUI app, as an Xcode project (or bring your own)
npx @swiftbrowser/cli check ~/Projects/MyApp    # what the app imports, and a fast compile against the shim
npx @swiftbrowser/cli dev ~/Projects/MyApp      # http://localhost:5173, rebuilds on save
npx @swiftbrowser/cli test ~/Projects/MyApp     # the app's unit tests, in WebAssembly
npx @swiftbrowser/cli build ~/Projects/MyApp    # ./dist: a static site to deploy anywhere

All twenty Hacking with SwiftUI projects run from their Xcode projects without a change, and so do Apple's Backyard Birds sample (its iPhone target, with its three packages and its PDF artwork) and Food Truck sample (its iOS app, with its package, Canvas header and tables).

Documentation#

For app developers:

  • Getting started: from a new app or an Xcode project to a deployed site.
  • CLI reference: commands, swiftbrowser.json, Foundation, frameworks, Core ML, Swift packages.
  • Compatibility: the SwiftUI, SwiftData and framework APIs the browser build supports.
  • RealityKit: 3D scenes and games with RealityView, entities, components and systems, drawn by three.js (experimental).
  • @swiftbrowser/react: SwiftUI's components for pure React/TypeScript web apps, drawn by the same renderer, and how to mix them with ordinary React (experimental).
  • @swiftbrowser/xcodeproj: the Xcode project reader, usable on its own.
  • SwiftBrowser Native and @swiftbrowser/native: the other direction, a @swiftbrowser/react app running in JavaScriptCore inside an iOS app, drawn by real SwiftUI over the same ops (experimental; the design is in docs/native.md).

How it works:

  • Architecture and the ops protocol between Swift and the renderer.
  • The web renderer and its dev server.
  • The in-memory renderer: the ops drawn to a PNG with no browser, by the same layout engine and a Rust rasterizer in WebAssembly, for screenshots in agents, workers and tests (experimental).
  • History: what each development phase added.
  • The in-browser compiler: the plan for a playground that compiles with the real Swift compiler running as WebAssembly, no server involved.

Working on SwiftBrowser itself: CONTRIBUTING.md (local development, CI, deploying the examples, releasing to npm, repository layout).

All of it is also published at https://swiftbrowser-docs.pages.dev (DocsSite/), where every page, or the whole site at once, can be copied as Markdown.

Packages#

Package What it is
@swiftbrowser/cli doctor, check, dev and build for an Xcode project or a folder of Swift sources
@swiftbrowser/web the renderer, the dev server and the static site build
@swiftbrowser/xcodeproj a dependency-free project.pbxproj reader
@swiftbrowser/swift the Swift package (the SwiftUI shim, Charts, SwiftData, FoundationBridge and the framework stand-ins) the CLI builds against
@swiftbrowser/react SwiftUI's components for pure React/TypeScript apps, drawn by the same renderer (experimental)
@swiftbrowser/native the runtime, entry and bundler that run a @swiftbrowser/react app in JavaScriptCore inside an iOS app, drawn by real SwiftUI through the Native/ Swift package (experimental)
@swiftbrowser/raster the in-memory renderer: an op stream drawn to a PNG with no browser, by the web renderer's layout engine and a Rust rasterizer compiled to WebAssembly (experimental; the design is in docs/raster.md)