Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Construct an immersive environment for visionOS

At a glance

Item Summary
Purpose Build efficient custom worlds for your app.
App architecture MeadowApp is a compact SwiftUI scene shell; ImmersiveView loads the authored meadow from RealityKitContent, leaving environment composition in Reality Composer Pro assets.
Main patterns SwiftUI scene composition, UI–RealityKit bridge, Authored-content boundary, Authored-scene boundary
Project style Code-light sample with 6 scanned Swift file(s) and 193 implementation line(s).

Project structure

Source bundle/
├── Meadow/MeadowApp.swift  # MeadowApp
├── Meadow/ImmersiveView.swift  # ImmersiveView
├── Meadow/ContentView.swift  # ContentView
├── Meadow/EnvironmentLoader.swift  # EnvironmentLoader
├── Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift
└── Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json  # authored RealityKit content

Structure observations

  • The runtime boundary is WindowGroup + ImmersiveSpace; the tree is pruned to lifecycle, state, framework, rendering, and ECS files.
  • The source is Swift; its compact size does not erase any explicit owner shown below.
  • Authored .reality, .rkassets, or Reality Composer Pro content is an implementation boundary; Swift loads or drives it rather than reproducing its entity graph.

Overall architecture

Reference code

Meadow/MeadowApp.swift:11 — executable or app-lifecycle anchor.

struct MeadowApp: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It separates lifecycle, presentation, stable state, framework/session work, and the RealityKit entity graph.

Ownership and state

Ownership evidence

Meadow/MeadowApp.swift:13 — representative stored state or nearest verified lifecycle anchor.

@main
struct MeadowApp: App {
    // ...
    @State var loader = EnvironmentLoader()
    // ...
}
Owner Object or state Relationship Mutation authority
MeadowApp EnvironmentLoader as loader creates and retains The owning type coordinates writes
ImmersiveView ScenePhase as scenePhase receives a shared or non-owning reference The upstream owner controls lifetime; this scope may invoke the exposed mutable API
ContentView Entity as root creates and retains The owning type coordinates writes
EnvironmentLoader Entity as entity receives a shared or non-owning reference The upstream owner controls lifetime; this scope may invoke the exposed mutable API

Ownership is intentionally narrow: environment, bindings, observed objects, weak links, and delegate callbacks do not prove lifetime ownership. Initialized stored services and wrapper-managed state do; entities added inside a RealityKit content closure belong to that entity graph.

Class and protocol design

Type Responsibility Depends on or conforms to
MeadowApp (Meadow/MeadowApp.swift:11) Declares app lifecycle and top-level dependency lifetime. App
ImmersiveView (Meadow/ImmersiveView.swift:11) Presents UI and forwards gestures or lifecycle events. View
ContentView (Meadow/ContentView.swift:12) Presents UI and forwards gestures or lifecycle events. View

The reviewed source defines no local substitution protocol. Its App, View, delegate, renderer, ARKit, and RealityKit conformances are framework-facing contracts, so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private weak var entity: Entity? (Meadow/EnvironmentLoader.swift:16) private Use stays inside the declaration and same-file extensions permitted by Swift. Inference: Hide lifecycle-sensitive state or an implementation step.
public let realityKitContentBundle = Bundle.module (Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift:11) public The symbol is available to importing modules, subject to its containing type’s visibility. Inference: Expose an intentional package or cross-target surface.

No reviewed Swift access example uses fileprivate, private(set), open; declarations without a modifier are internal.

Reference code

Meadow/EnvironmentLoader.swift:16 — representative visibility boundary.

actor EnvironmentLoader {
    // ...
    private weak var entity: Entity?
    // ...
}

Logic ownership and placement

Logic Owning type or file Placement rationale
Application/command lifecycle MeadowApp The executable boundary determines app, controller, scene, or command lifetime.
Presentation and user intent ImmersiveView The view/controller forwards input and owns only presentation-local work.
Authored scene composition Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json Reality Composer Pro owns hierarchy, materials, animation, or behavior that Swift loads and coordinates.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition Meadow/MeadowApp.swift:11 Keeps windows, volumes, and immersive presentation visible at the app boundary.
UI–RealityKit bridge Meadow/ImmersiveView.swift:19 Builds or hosts the RealityKit entity graph from SwiftUI or a platform view/controller lifecycle.
Authored-content boundary Meadow/ImmersiveView.swift:9 Treats Reality Composer Pro assets as implementation and keeps Swift responsible for loading and runtime coordination.
Authored-scene boundary Meadow/MeadowApp.swift:13 Treats Reality Composer Pro content as the environment implementation and keeps Swift responsible for loading and lifecycle.

Naming conventions

  • Role suffixes are evidence, not decoration: App: MeadowApp; View: ContentView, ImmersiveView.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: getEntity.
  • Files and folders use primary-type or responsibility names such as Views, Models, Rendering, Components, Systems, Packages, or feature names where those boundaries exist.

Architecture takeaways

  • Treat MeadowApp as the owner of executable or scene lifecycle, not automatically as the owner of every entity created later.
  • Keep view/controller input near presentation, but move sessions, reconstruction, capture, rendering, shared game state, or documents into stable owners when their lifetime exceeds one callback or render pass.
  • Count authored Reality Composer Pro assets as implementation; the Swift shell is not the complete architecture.
  • The compact source does not justify repository, coordinator, or protocol layers beyond boundaries the sample actually declares.

Source map

Source file Relevant symbols
Meadow/MeadowApp.swift:11 MeadowApp
Meadow/ImmersiveView.swift:11 ImmersiveView
Meadow/ContentView.swift:12 ContentView
Meadow/EnvironmentLoader.swift:13 EnvironmentLoader
Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift:1 Feature implementation