Treat Xcode Previews as UI Unit Tests When Building Apple Apps with AI Agents

Instead of letting coding agents rebuild and Cmd+R the whole app on every UI tweak, have them write multiple #Preview variants through the Xcode MCP Bridge, test interactions directly in Canvas, and pair Previews with an API mock layer to verify real JSON states.

On this page
  1. Compare and Iterate Directly in Xcode Canvas via MCP Bridge
  2. Think of #Preview as a UI Unit Test
  3. Go One Step Further: Pair Previews with an API Mock Kit
  4. A Few Practical Habits That Keep Previews Fast
  5. Closing Thought

When building iOS and macOS apps with coding agents like Antigravity, Claude Code, or Codex, there is a default loop that quietly burns a lot of time: the agent edits a few lines of UI code, builds the whole app, and hits Cmd+R to launch the simulator or device so you can check the result.

That loop is fine when you are testing an end-to-end flow. It is a terrible loop when you are still exploring UI and interaction design. Say you want a battery charging chart and you are not sure which visual style or scrubbing interaction feels best. Rebuilding the entire app, cold-starting the simulator, and clicking back to the screen on every single iteration kills your momentum.

In Apple’s ecosystem, there is a much tighter feedback loop already sitting in Xcode: have the agent write #Preview macros directly in your project and treat Xcode Previews as UI unit tests.

Compare and Iterate Directly in Xcode Canvas via MCP Bridge

Coding agents connected through Xcode’s MCP Bridge can write and update Swift files right inside your workspace. When you are unsure about a layout or interaction style, ask the agent to generate two or three #Preview variants instead of modifying the app flow and doing a full run.

For example, when exploring a charging chart, the agent can drop multiple styles and an interactive state side by side in the same file:

import SwiftUI

#Preview("Style A - Smooth Area") {
    ChargingChartView(samples: .mockFastCharge, style: .smoothArea)
        .padding()
}

#Preview("Style B - Segmented Bars") {
    ChargingChartView(samples: .mockFastCharge, style: .segmentedBars)
        .padding()
}

#Preview("Interactive - Scrubbing") {
    @Previewable @State var selectedIndex: Int? = 3
    ChargingChartView(
        samples: .mockFastCharge,
        style: .smoothArea,
        selectedIndex: $selectedIndex
    )
    .padding()
}

From there, everything happens in Xcode’s Preview Canvas:

  1. Switch between the preview tabs at the top to compare visual options side by side.
  2. Click, drag, and scrub directly on the Canvas to feel the interaction before committing to it.
  3. Once you pick a direction, iterate on the small details—spacing, animation curves, highlight states—by telling the agent what to adjust and watching the Canvas update immediately.

This “describe change -> agent edits -> interact in Canvas” loop is dramatically lighter than rebuilding and relaunching the whole app on every pass.

Think of #Preview as a UI Unit Test

Ever since Swift introduced the #Preview macro, previews stopped being a quick layout scratchpad. Structurally, each #Preview works like a visual, interactive unit test case:

  • Deterministic input: a specific data model, appearance (Dark/Light), size class, or initial state.
  • Immediate output: rendered pixels and live interaction right in front of you.
  • Tight isolation: no login flow, no multi-screen navigation, and no full app startup overhead.

When you design screens around previewable test cases, most of an app’s views can be rendered, clicked through, and refined inside Xcode Canvas. You save full builds and simulator/device runs for integration and final verification instead of using them as a pixel-pushing loop.

Go One Step Further: Pair Previews with an API Mock Kit

Static mock structs are enough for isolated components, but real screens depend on backend payloads. Often you want to see what the UI actually renders after a specific JSON payload, empty response, 401, or 500 goes through your real networking and decoding layer.

In a previous commercial iOS project, I built a local API Mock Kit on top of URLProtocol. It intercepts requests underneath URLSession and maps each route to switchable MockVariant states—default data, slow loading, unauthorized, server error, timeout, and offline—so the view model, networking stack, JSON decoder, and error mapper all run for real.

Combining that pattern with #Preview turns Canvas into a live state matrix driven by real JSON fixtures:

#Preview("Overview - Loaded") {
    MockKit.activate(.default, for: .itemOverview)
    return ItemOverviewScreen(source: .mock)
}

#Preview("Overview - Server Error 500") {
    MockKit.activate(.serverError, for: .itemOverview)
    return ItemOverviewScreen(source: .mock)
}

#Preview("Overview - Offline") {
    MockKit.activate(.offline, for: .itemOverview)
    return ItemOverviewScreen(source: .mock)
}

Instead of injecting hand-crafted view model stubs that bypass decoding, every preview exercises the same JSON fixtures and error paths the app uses at runtime. It makes early UI demos and AI-assisted edge-case polishing much faster to verify.

A Few Practical Habits That Keep Previews Fast

A few small details keep this workflow from breaking down in day-to-day work:

  • Do not wrap top-level #Preview blocks in #if DEBUG. Swift 5.9+ strips #Preview automatically in Release builds, and #if DEBUG can interfere with Xcode’s preview scanner.
  • Guard heavy app startup work. If your app entry point kicks off background services or network calls, skip them when ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" so Canvas stays snappy.
  • Prune throwaway variants after deciding. Let the agent generate three or four competing styles during exploration, then remove the discarded ones and keep only the canonical state previews (loaded, empty, error, interactive) as living UI test cases.

Closing Thought

When building native Apple apps with AI agents, the bottleneck is rarely how fast the model writes Swift—it is how heavy the human review loop is.

Treating #Preview as a UI unit test, having the agent write variants straight into Xcode via MCP Bridge, and backing complex screens with a JSON-driven API mock layer turns UI iteration from a slow Cmd+R cycle into a fast, tactile Canvas loop.