Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Implementing SharePlay for immersive spaces in visionOS

At a glance

Item Summary
Purpose Enable collaborative spatial experiences by using SharePlay to synchronize 3D content among participants.
App architecture SharePlayCubeApp composes ImmersiveSpace; AppModel owns GroupActivities session state and CubeImmersiveView reflects synchronized transitions.
Main patterns SwiftUI scene composition, SwiftUI–RealityKit bridge, Explicit immersive-space lifecycle, Observable state owner, Async update consumer, Shared-session model
Project style Code-rich sample with 7 scanned Swift file(s) and 452 Swift line(s); resources and generated assets are excluded from those counts.

Project structure

Source bundle/
└── GroupActivities-SharePlayCube/
    ├── SharePlayCubeApp.swift  # SharePlayCubeApp
    ├── Models/
    │   ├── SessionController.swift  # SessionController
    │   ├── AppModel.swift  # AppModel
    │   ├── CubeActivityMessagingTypes.swift  # UpdateColorMessage, MessageUpdateCount, CubeColor
    │   └── ChangeColorActivity.swift  # ChangeColorActivity
    └── Views/
        ├── CubeImmersiveView.swift  # CubeImmersiveView
        └── SharePlayAttachmentView.swift  # SharePlayAttachmentView

Structure observations

  • The runtime boundary is ImmersiveSpace; the pruned tree lists only files that explain lifecycle, state, or framework integration.
  • App code is split into role-named views, models, managers, providers, components, or systems.

Overall architecture

Reference code

GroupActivities-SharePlayCube/SharePlayCubeApp.swift:12 — the app or executable entry declares the outer scene lifecycle.

struct SharePlayCubeApp: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.

Ownership and state

Ownership evidence

GroupActivities-SharePlayCube/SharePlayCubeApp.swift:13 — representative stored state or the nearest verified lifecycle anchor.

    @State private var appModel = AppModel()
Owner Object or state Relationship Mutation authority
SharePlayCubeApp AppModel as appModel creates and retains Only the declaring scope writes
CubeImmersiveView AppModel as appModel receives a shared/non-owning reference The upstream owner controls lifetime; this scope may invoke its mutable API
CubeImmersiveView Entity as rootEntity creates and retains Only the declaring scope writes
AppModel SessionController as sessionController stores and coordinates Only the declaring scope writes

Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.

Class and protocol design

Type Responsibility Depends on or conforms to
SharePlayCubeApp (GroupActivities-SharePlayCube/SharePlayCubeApp.swift:12) Declares app scenes and top-level dependency lifetime. App
CubeImmersiveView (GroupActivities-SharePlayCube/Views/CubeImmersiveView.swift:12) Presents UI and forwards gestures or lifecycle events. View
AppModel (GroupActivities-SharePlayCube/Models/AppModel.swift:17) Owns observable feature state and domain transitions. Concrete framework collaborators
SessionController (GroupActivities-SharePlayCube/Models/SessionController.swift:15) Coordinates long-lived framework or cross-view work. Concrete framework collaborators
SharePlayAttachmentView (GroupActivities-SharePlayCube/Views/SharePlayAttachmentView.swift:13) Presents UI and forwards gestures or lifecycle events. View

The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private(set) var color: CubeColor = .red (GroupActivities-SharePlayCube/Models/AppModel.swift:27) private(set) The getter keeps its wider visibility, while mutation stays inside the declaring type and its permitted same-file extensions. Inference: Protect a state invariant while allowing observation.
private var sessionController: SessionController? (GroupActivities-SharePlayCube/Models/AppModel.swift:19) private Use is restricted to the declaration and same-file extensions permitted by Swift. Inference: Hide implementation details and lifecycle-sensitive state.

No reviewed declaration uses fileprivate, public, open; unmodified Swift declarations are internal.

Reference code

GroupActivities-SharePlayCube/Models/AppModel.swift:27 — representative visibility boundary.

    private(set) var color: CubeColor = .red

    init() {
        // ...
    }

Logic ownership and placement

Logic Owning type or file Placement rationale
Scene declaration and dependency lifetime SharePlayCubeApp The App/entry boundary determines window, volume, and immersive-space lifetime.
Presentation, attachments, and gestures CubeImmersiveView SwiftUI view code forwards user intent and RealityView lifecycle events.
Shared feature state and commands AppModel A role-named owner prevents sibling views from duplicating transitions.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition GroupActivities-SharePlayCube/SharePlayCubeApp.swift:12 Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary.
SwiftUI–RealityKit bridge GroupActivities-SharePlayCube/Views/CubeImmersiveView.swift:34 Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures.
Explicit immersive-space lifecycle GroupActivities-SharePlayCube/SharePlayCubeApp.swift:12 Makes immersive presentation a scene transition rather than hidden global state.
Observable state owner GroupActivities-SharePlayCube/Models/AppModel.swift:17 Shares feature state across multiple views without moving framework resources into view values.
Async update consumer GroupActivities-SharePlayCube/Models/SessionController.swift:79 Consumes provider or event streams in cancellable structured tasks.
Shared-session model GroupActivities-SharePlayCube/Models/SessionController.swift:18 Centralizes SharePlay activation, messages, and participant state.

Naming conventions

  • Role suffixes are evidence, not decoration: App: SharePlayCubeApp; Model: AppModel; Controller: SessionController; View: CubeImmersiveView, SharePlayAttachmentView.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: endSession, observeUpdateColorMessages, syncColorChange, observeActiveRemoteParticipants, setColor, observeGroupSessions, changeColor, setUpRootEntity.
  • Files generally match their primary type; Views, Models, Managers, Providers, Components, Systems, and Packages folders describe architectural roles where present.

Architecture takeaways

  • Treat SharePlayCubeApp as the owner of scene declarations, not as the owner of every RealityKit entity created later.
  • Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.
  • Model immersive-space open, transition, and close states explicitly so windows and immersive content cannot drift apart.

Source map

Source file Relevant symbols
GroupActivities-SharePlayCube/SharePlayCubeApp.swift:12 SharePlayCubeApp
GroupActivities-SharePlayCube/Models/SessionController.swift:15 SessionController
GroupActivities-SharePlayCube/Models/AppModel.swift:17 AppModel
GroupActivities-SharePlayCube/Models/CubeActivityMessagingTypes.swift:12 UpdateColorMessage, MessageUpdateCount, CubeColor
GroupActivities-SharePlayCube/Views/CubeImmersiveView.swift:12 CubeImmersiveView
GroupActivities-SharePlayCube/Views/SharePlayAttachmentView.swift:13 SharePlayAttachmentView
GroupActivities-SharePlayCube/Models/ChangeColorActivity.swift:12 ChangeColorActivity