Code structure: what goes where
How MVC, MVVM, VIPER, coordinators and unidirectional apps carry across platforms, and how to balance #if os, packages and per-platform files.
Keep models, state and logic in a shared core. Give each platform a thin layer for views, navigation and system integration. Adding a platform shouldn't mean forking your logic. The platform pages say what each platform needs; this page says where the code goes.
The one rule
Shared core, platform edges. Models, state, business rules, persistence, networking and your App Intents' work go in the core. Views, navigation, input (touch, focus, pointer, the crown, the hinge) and system integration (scenes, menus, templates, extensions) go at the edges.
| Layer | Goes with the app to every platform? | Notes |
|---|---|---|
| Models and domain types | Yes | No UIKit, AppKit or SwiftUI imports. Use Foundation, CoreGraphics and plain value types |
| State and business logic | Yes | Reducers, interactors, use cases, view models that don't name a UI type |
| Persistence and sync | Yes, but not into every process | Extensions and the watch read a small snapshot, not your database (Every platform) |
| App Intents and entities | Yes | In a module the app and its extensions link. perform() calls the same functions your buttons do |
| Navigation and routing | Per platform | Windows on Mac and Vision Pro, focus on TV, templates in CarPlay. The biggest difference between platforms lives here |
| Views | Per platform where they differ | Share a view while it fits; split it when the #ifs start piling up |
Composition root (App, scenes, scene delegates) |
Per platform | Where you choose which views and services each platform gets |
By architecture pattern
Every common pattern already has the seam. The work is putting the platform code on the right side of it.
MVC
- Shared: the model. Nothing else, unless you pull it out.
- The trap: the "massive view controller" holds logic that every platform
needs, inside a type that's UIKit-only (and a native Mac app has
NSViewController, notUIViewController). - To add a platform: before adding a platform, pull non-view logic out of controllers into plain types the model layer owns. What's left in the controller is the part you'd rewrite per platform anyway.
MVVM
- Shared: models and view models, if the view models don't name UI types.
- The trap: a view model that exposes
UIImage,UIColororUIFont, or reaches forUIApplication.shared. That one import makes it iOS-only. - To add a platform: expose platform-neutral values (
Data,CGImage, a color name, SwiftUI'sImageandColorif every platform's views are SwiftUI). Make one view model per window or scene, not one per app: Mac, iPad and Vision Pro all open several windows at once, and a singleton view model shows the same thing in all of them.
VIPER (and Clean Architecture)
- Shared: Entity, Interactor, and most of the Presenter.
- Per platform: the View, and especially the Router.
- To add a platform: the Router is your best friend here. Navigation is what changes most between platforms (a new window on the Mac, a focus move on TV, pushing a template in CarPlay), and VIPER already isolates it. Give each platform its own Router behind the same protocol.
Coordinators
- Shared: whatever the coordinator decides.
- Per platform: how the decision is shown.
- To add a platform: split "the flow" (what comes next) from "the presentation" (push, sheet, window, template). A Mac coordinator opens a window where an iPhone one pushes a screen.
Unidirectional (TCA, Redux-style, a state + coordinator)
- Shared: state, actions and reducers. This is the most portable shape there is.
- Per platform: views, plus the clients (dependencies) that touch the system.
- To add a platform: inject platform services (haptics, photo library, contacts, the
watch connection) as dependencies with a live version per platform and a no-op
where the platform doesn't have it. The card game Deal Me In works this way: one
TableState, oneTableCoordinator, and solo play, pass-and-play, phones around a table and the TV are all different views of it.
Platform by platform: where the new code lands
| Platform | What's new | Which layer takes it |
|---|---|---|
| Widgets and App Intents | Separate processes, tight budgets | A shared intents module plus a small snapshot file. Never your view models or your database stack |
| iPad and iPhone Duo | Multiple windows, any size | Views (layout from the space you have) and scene state (one per window). The core doesn't change |
| Mac | Windows, menus, files, drag and drop | Composition root (scenes, .commands), routing (open a window), per-window state found with focusedSceneValue. Document types belong in the core |
| Apple Watch | A different process on a different device | A small shared snapshot type and one sync point. Views are new. Don't bring the persistence stack |
| Apple TV | Focus and the remote, no WidgetKit | Views and routing. Exclude widget and intents-UI extensions with platform filters. The core comes along unchanged |
| Vision Pro | Windows in space | Same as the Mac: per-window state, routing that opens windows. Fence iOS-only visual APIs in the views |
| CarPlay | Templates, not your views; may launch with no window | A second composition root (the CarPlay scene delegate) and a template layer that reads the same state. Start-up work can't live in a SwiftUI view's .task, because there may be no view |
#if os, packages, or something else
The goal: adding tvOS or macOS gives you a short list of real decisions, not a hundred errors and warnings that scare you off.
Three of my apps show what happens otherwise. Each has no shared package,
and each carries 140–220 platform #ifs spread through a third or more of its
files. The fourth keeps its engine in a Swift package with zero #if os and no
UI imports, so every platform fence sits in the app layer, where the platforms
actually differ.
Pick the lightest tool that keeps the difference at the edge:
- A core package with no UI imports. Models, state and logic in a local Swift
package that imports only
Foundation(and friends likeCoreGraphics). It either compiles for a platform or tells you exactly which line doesn't. This is the single biggest thing you can do before adding a platform. - Build every platform from day one. Add the platform to the build (or to CI) before you plan to ship there, and keep it green. Fifty errors on the day you try tvOS are just the errors you'd have fixed one at a time if it had been building all along. Build every platform on every change: a project-file change for one platform can quietly break another.
- Fence by capability, not by device.
#if canImport(WidgetKit)says what you actually mean, and it keeps working when a framework arrives on a new platform. Use#if os(...)when the platform's behavior differs, not as a stand-in for "this framework exists." - Separate files beat
#ifsalad. When a view differs a lot on one platform, give it its own file (PlayerView+tvOS.swift) and include that file only for that platform: platform filters on the file in Xcode, or in a package, a separate target that you depend on withcondition: .when(platforms: [.tvOS]). One big fork in one place is easier to read than twenty small ones in every view. - Protocols for system services. Haptics, the photo library, contacts, the
watch connection: one protocol in the core, one implementation per platform,
and a no-op where there isn't one. The
#ifhappens once, where you choose which implementation to inject. - Fence the usual suspects once. The first errors on a new platform are almost
always the same few:
UIScreen.main,UIDeviceidiom checks, haptics,UIApplication.shared.open, iOS-only visual styles (iOS 26's glass on Vision Pro), and gestures, popovers, hover and swipe actions on TV. Wrap each in one small helper or modifier instead of fencing every call site. - Availability is not a platform check.
if #availableand@availableare for OS versions.#if osand#if canImportare for whether something exists at all. Mixing them up is where a lot of the warnings come from. - Extensions need platform filters too. A widget extension embedded in an app that also builds for tvOS needs filters on both the dependency and the embed, or the tvOS build breaks.
When is #if os fine? For a line or two inside a view that's otherwise the
same on every platform. When the fences in one file start to outnumber the shared
lines, that file wants to be two files.
A quick health check. Count the #if os(...) lines in your core types (models,
state, logic). The goal is zero. Then look at where the rest are: if they're
spread across every view, start with steps 5 and 6.