Getting started#
SwiftBrowser runs an iOS SwiftUI app in the browser from its unchanged sources. The app compiles to WebAssembly against an API-compatible SwiftUI shim and draws inside an iPhone frame on a desktop, or full screen on a phone. You keep building the same project in Xcode for iOS; the browser build is a second target that needs no Mac.
This page takes an app, a new one or one you already have, from zero to a deployed static site. The CLI reference has every option, and Compatibility lists what the shim supports.
Requirements#
- Node 22 or later.
- The Swift SDK for WebAssembly and Binaryen's
wasm-opt.doctor --installfetches both. - Optionally, Swift 6.4, installed with swiftly
(
swiftly install 6.4.0), or any 6.4 toolchain whoseusr/binis inSWIFT_TOOLCHAIN_BIN. When it is installed the CLI builds with it, which is faster; without it, the CLI builds with the Swift compiler built as WebAssembly, which comes with it from npm (--compilerpicks one).
Linux and macOS both work. Nothing is installed globally besides the Swift
SDK; Binaryen and cached data live in ~/.cache/swiftbrowser.
npx @swiftbrowser/cli doctor --install
doctor without --install only reports what is missing.
Start a new app#
Skip this with an app of your own. Otherwise init writes one:
npx @swiftbrowser/cli init MyApp
cd MyApp
MyApp/ now holds the project Xcode 16's iOS App template makes with tests:
MyApp.xcodeproj, with an app target for iOS 18 and a unit-test target, the
folder MyApp/ with MyAppApp.swift, a ContentView.swift with a tap
counter, and an asset catalog, and the folder MyAppTests/ with a Swift
Testing test of the counter's title. A .gitignore and a README come with
it. The steps below work on it as is (use . for the project path), and so
does Xcode: open MyApp.xcodeproj, and ⌘U runs the test. A Swift file you
add under MyApp/ or MyAppTests/, from Xcode or any editor, is part of
both builds.
The app name comes from the directory (init my-app makes MyApp), or set
it with --name. --bundle-id sets the bundle identifier, which is
com.example.MyApp by default. init also works in an existing directory,
such as a fresh git clone, as long as MyApp.xcodeproj, MyApp/ and
MyAppTests/ are not taken there.
1. Check the app#
Point the CLI at an .xcodeproj, a folder that holds one (directly or one
level down), or a plain folder of Swift sources:
npx @swiftbrowser/cli check ~/Projects/MyApp
check lists every module the app imports as supported, rewritten or
unsupported (with the usual way around each), then compiles the app natively
against the shim. It takes seconds and builds no WebAssembly. Every error
names your file and line. Anything the shim lacks is a compile error, never a
wrong rendering at runtime.
When a project has several app targets, pick one with --target <name>.
2. Work around what is missing#
Code that cannot run in the browser goes behind the SWIFTBROWSER compilation
condition, which the CLI defines, much like #if os(iOS):
#if SWIFTBROWSER
// A stand-in for the browser.
Text("Maps are not available in the browser yet")
#else
Map(position: $position)
#endif
Xcode never defines SWIFTBROWSER, so the iOS build is unaffected.
The browser runs the app as an iOS app: #if os(iOS) holds. To run a Mac
app, build with --platform macos (or "platform": "macos" in
swiftbrowser.json): #if os(macOS) holds instead, SWIFTBROWSER_MACOS is
defined beside SWIFTBROWSER, and the page shows the app in a Mac window.
The page then draws the Mac idiom (docs/notes/macos.md), and import AppKit compiles: the shim's AppKit is its UIKit's colors, images, fonts,
pasteboard and representables under their Mac names (NSColor,
NSImage, NSWorkspace.shared.open, @NSApplicationDelegateAdaptor,
NSViewRepresentable); windows, menus and panels are not there, so code
that needs them stays behind #if !SWIFTBROWSER. In an iOS build
canImport(AppKit) stays false.
3. Run it with live reload#
npx @swiftbrowser/cli dev ~/Projects/MyApp
The dev server serves the app at http://localhost:5173, in an iPhone frame
with controls for dark mode and the text size. Saving a Swift file rebuilds the
module and reloads the page with the app's @State carried over where the
view tree still matches.
To try it on a phone, add --host to expose the server on your network and
scan the QR code the command prints. On a phone the app drops the iPhone frame
and uses the real screen and safe areas; in Safari, Share → Add to Home Screen
runs it full screen.
Other useful flags: --port, --open, and --verbose for the full compiler
output.
4. Run the tests#
npx @swiftbrowser/cli test ~/Projects/MyApp
test builds the app's unit tests for WebAssembly and runs them under
Node, in the same runtime as the app in the browser. It takes about ten
seconds for a new app. XCTest and Swift Testing both work. The tests come from
the Xcode project's unit-test targets for the app, which can
@testable import it, or from the Tests and *Tests folders of a plain
folder of sources. A failing test makes the command fail, so it also works as
a CI step.
5. Build a static site#
npx @swiftbrowser/cli build ~/Projects/MyApp -o dist
build makes a release module, optimized and stripped with wasm-opt, and a
complete static site around it: the app at /, the asset catalogs and a web
app manifest. The site is the app: it fills the window on every screen,
drawn as a Mac app in a desktop browser at 700 px or more (a mouse or a
trackpad; docs/notes/macos.md) and as iOS on a phone or a tablet;
?device=ipad keeps the iPad layout on a desktop, and ?mode=frame shows
the iPhone frame instead. A small app is about 17 MB, 4.5 MB with brotli.
Serve dist/ from any static host that sends .wasm files as
application/wasm (Cloudflare Pages, Netlify, GitHub Pages and S3 all do).
What the CLI does to your project#
Nothing outside <project>/.swiftbrowser/, which you should add to
.gitignore (a project from init already has it there). The CLI reads the Xcode project, copies the app target's sources
into a generated SwiftPM package there, adjusts the copies for the shim
(keeping line numbers, so diagnostics point at your files) and builds that.
See What happens to your project.
Configuration#
Most projects need none. When the Xcode project is not enough, or the app is
a plain folder of sources, an optional swiftbrowser.json next to it names
the sources, resources, asset catalogs, icon, ICU locales and extra
compilation conditions. See
swiftbrowser.json.
Data that persists#
What an iOS app keeps on the device, the browser keeps per app:
| On iOS | In the browser | Reset with |
|---|---|---|
UserDefaults, @AppStorage |
localStorage | clearing site data |
| SwiftData | IndexedDB, sb-swiftdata:<App> |
?swiftdata=reset |
Files in the app's home (URL.documentsDirectory, …) |
IndexedDB, sb-files:<App> |
?files=reset |
Next#
- CLI reference: commands, Foundation and ICU data, frameworks, Core ML, Swift packages.
- Compatibility: the SwiftUI and framework APIs that work.
- Architecture: how the shim, the render ops and the renderer fit together.