Sample CodeiOS, iPadOS, Mac Catalyst, visionOSReviewed 2026-07-21View on Apple Developer

Connecting iPadOS and visionOS apps over the local network

At a glance

Item Summary
Purpose Build an iPadOS companion app to control your visionOS app.
App architecture A Swift sample with the source-visible chain CompanionAppServerContentViewRobotViewModelClientSwiftUI / PeerToPeerMessaging APIs.
Main patterns Model-View-ViewModel, Protocol-oriented abstraction, Actor isolation
Project style 23 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: async declaration or closure, actor, Task, Sendable or @Sendable, @MainActor; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages SwiftUI, PeerToPeerMessaging, OSLog, Network, RealityKit, Remote package https://github.com/apple/swift-certificates.git; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── iPadCompanionApp/
    ├── Companion/
    │   ├── CompanionApp.swift
    │   └── Views/
    │       ├── ControlPanelView.swift
    │       ├── RobotAndControlsView.swift
    │       └── ServerContentView.swift
    ├── RobotConfigurationExperience/
    │   ├── RobotConfigurationExperienceApp.swift
    │   └── Views/
    │       └── ClientContentView.swift
    ├── SharedContent/
    │   └── Models/
    │       ├── NetworkCommand.swift
    │       └── RobotViewModel.swift
    └── Packages/
        └── PeerToPeerMessaging/
            └── Sources/
                └── PeerToPeerMessaging/
                    ├── ConnectionTypes.swift
                    ├── Actors/
                    │   └── Client.swift
                    ├── TLS/
                    │   └── CertificateTrustManager.swift
                    └── PeerMessagingController.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 5 project/configuration file(s) and 26 source declaration(s).

Overall architecture

Reference code

iPadCompanionApp/Companion/CompanionApp.swift:11 — architecture anchor

@main
struct CompanionApp: App {
    @State private var serverController: PeerMessagingController<Server<NetworkCommand>>
    @State private var robotViewModel: RobotViewModel

    init() {
        let serverController = PeerMessagingController<Server<NetworkCommand>>()
        let robotViewModel = RobotViewModel(serverController)

        self.serverController = serverController
        self.robotViewModel = robotViewModel
    }

    var body: some Scene {
        WindowGroup {
            ServerContentView()
                .environment(serverController)
                .environment(robotViewModel)
                .ignoresSafeArea()
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into visionOS.

Ownership and state

Ownership evidence

iPadCompanionApp/Companion/CompanionApp.swift:13 — stored dependency or nearest verified ownership anchor

@main
struct CompanionApp: App {
    @State private var serverController: PeerMessagingController<Server<NetworkCommand>>
    // ...
}
Owner Object or state Relationship Mutation authority
CompanionApp PeerMessagingController (serverController) owns wrapper-managed state Owning lexical scope
CompanionApp RobotViewModel (robotViewModel) owns wrapper-managed state Owning lexical scope
RobotConfigurationExperienceApp PeerMessagingController (clientController) owns wrapper-managed state Owning lexical scope
RobotConfigurationExperienceApp RobotViewModel (robotViewModel) owns wrapper-managed state Owning lexical scope

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. iPadCompanionApp/Companion/Views/RobotAndControlsView.swift:45
Actor isolation actor The cited type is actor-isolated; this does not select a background thread. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift:14
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift:194
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/ConnectionTypes.swift:19
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/PeerMessagingController.swift:16

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

iPadCompanionApp/Companion/Views/RobotAndControlsView.swift:45 — representative execution boundary

    private func handleRobotEvents() async {
        for await case .robotEvent(let event) in serverController.incomingMessages {
            robotViewModel.handleRobotEvent(event)
        }
    }

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. iPadCompanionApp/Companion/CompanionApp.swift:13
State propagation @Observable Observation macro publishes source-visible changes. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/PeerMessagingController.swift:16
Package manifest Remote package https://github.com/apple/swift-certificates.git The manifest declares a remote URL or registry dependency; direct target use and transitive position are not inferred. iPadCompanionApp/Packages/PeerToPeerMessaging/Package.swift:26
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. iPadCompanionApp/Companion/CompanionApp.swift:8
Source import PeerToPeerMessaging The cited file imports this module; runtime use and architectural role are not inferred. iPadCompanionApp/Companion/CompanionApp.swift:9
Source import OSLog The cited file imports this module; runtime use and architectural role are not inferred. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift:9
Source import Network The cited file imports this module; runtime use and architectural role are not inferred. iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift:8
Source import RealityKit The cited file imports this module; runtime use and architectural role are not inferred. iPadCompanionApp/SharedContent/Extensions.swift:8

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Protocols/PeerMessagingProtocols.swift:17 — representative type boundary

public protocol PeerMessagingManager<Message>: Actor where Message: PeerToPeerMessage {
    associatedtype Message: PeerToPeerMessage
    // ...
}
Type Responsibility Depends on or conforms to
CompanionApp Application entry and top-level composition App
RobotConfigurationExperienceApp Application entry and top-level composition App
PeerMessagingManager Defines a capability or collaboration contract Concrete collaborators/imported frameworks
NetworkCommand Represents or runs a user/system command PeerToPeerMessage
RobotViewModel UI-facing state and feature coordination Concrete collaborators/imported frameworks
Client External-system or framework client Concrete collaborators/imported frameworks
CertificateTrustManager Long-lived feature or framework coordination Sendable
ControlPanelView User-interface presentation and input forwarding View
RobotAndControlsView User-interface presentation and input forwarding View
ServerContentView User-interface presentation and input forwarding View

The source explicitly defines local protocol relationships: NetworkCommandPeerToPeerMessage, RobotEventPeerToPeerMessage, MockNetworkActorPeerMessagingManager.

Access control

Symbol Access Verified effect Likely rationale
serverController (iPadCompanionApp/Companion/CompanionApp.swift:13) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
robotViewModel (iPadCompanionApp/Companion/CompanionApp.swift:14) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
handleRobotEvents (iPadCompanionApp/Companion/Views/RobotAndControlsView.swift:45) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
networkTask (iPadCompanionApp/Companion/Views/ServerContentView.swift:15) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.

Reference code

iPadCompanionApp/Companion/CompanionApp.swift:13 — representative boundary

@main
struct CompanionApp: App {
    @State private var serverController: PeerMessagingController<Server<NetworkCommand>>
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition CompanionApp, RobotConfigurationExperienceApp The source’s App suffix makes this role explicit.
External-system or framework client Client The source’s Client suffix makes this role explicit.
Represents or runs a user/system command NetworkCommand The source’s Command suffix makes this role explicit.
Long-lived feature or framework coordination CertificateTrustManager, PeerMessagingManager The source’s Manager suffix makes this role explicit.
User-interface presentation and input forwarding ClientContentView, ControlPanelView, RobotAndControlsView, RobotView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination RobotViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel iPadCompanionApp/SharedContent/Models/RobotViewModel.swift:16 Role-named view models keep UI-facing state or coordination outside view declarations.
Protocol-oriented abstraction iPadCompanionApp/SharedContent/Models/NetworkCommand.swift:12 A local protocol and concrete conformance create an explicit capability boundary.
Actor isolation iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift:14 A declared actor creates an explicit isolation boundary; its executor is not described as a background thread.

Naming conventions

  • Types: App: CompanionApp, RobotConfigurationExperienceApp; Client: Client; Command: NetworkCommand; Manager: CertificateTrustManager, PeerMessagingManager; View: ClientContentView, ControlPanelView, RobotAndControlsView, RobotView, RobotWindowView; ViewModel: RobotViewModel.
  • Protocols: PeerMessagingManager, PeerToPeerMessage.
  • Methods: toggleWindowPresentation, syncNewSelectedColor, toggleVolumeVisibility, reset, load, loadEntityFromRCPro, updateRobotAppearance, handleRobotEvent.
  • Files: iPadCompanionApp/Companion/CompanionApp.swift, iPadCompanionApp/RobotConfigurationExperience/RobotConfigurationExperienceApp.swift, iPadCompanionApp/SharedContent/Models/NetworkCommand.swift, iPadCompanionApp/SharedContent/Models/RobotViewModel.swift, iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift, iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/TLS/CertificateTrustManager.swift.

Architecture takeaways

  • CompanionApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, PeerToPeerMessaging, Network, RealityKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
iPadCompanionApp/Companion/CompanionApp.swift Cited implementation, SwiftUI state property wrapper, SwiftUI, PeerToPeerMessaging, CompanionApp
iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Protocols/PeerMessagingProtocols.swift PeerMessagingManager, PeerToPeerMessage
iPadCompanionApp/Companion/Views/RobotAndControlsView.swift Cited implementation, async declaration or closure, RobotAndControlsView
iPadCompanionApp/Companion/Views/ServerContentView.swift Cited implementation, ServerContentView
iPadCompanionApp/SharedContent/Models/RobotViewModel.swift RobotViewModel
iPadCompanionApp/SharedContent/Models/NetworkCommand.swift Cited implementation, NetworkCommand, RobotEvent
iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/Actors/Client.swift Client, actor, Task, OSLog, Network
iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/ConnectionTypes.swift Sendable or @Sendable, NetworkEvent, ConnectionEvent, NetworkState, NetworkServiceConstants, TLSError
iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/PeerMessagingController.swift @MainActor, @Observable, Feature implementation
iPadCompanionApp/Packages/PeerToPeerMessaging/Package.swift Cited implementation, Feature implementation
iPadCompanionApp/SharedContent/Extensions.swift RealityKit, Feature implementation
iPadCompanionApp/RobotConfigurationExperience/RobotConfigurationExperienceApp.swift RobotConfigurationExperienceApp
iPadCompanionApp/Packages/PeerToPeerMessaging/Sources/PeerToPeerMessaging/TLS/CertificateTrustManager.swift CertificateTrustManager
iPadCompanionApp/Companion/Views/ControlPanelView.swift ControlPanelView
iPadCompanionApp/RobotConfigurationExperience/Views/ClientContentView.swift ClientContentView
iPadCompanionApp/RobotConfigurationExperience/Views/RobotWindowView.swift RobotWindowView