Accessing the main camera
At a glance
| Item | Summary |
|---|---|
| Purpose | Add camera-based features to enterprise apps. |
| App architecture | AccessingTheMainCameraApp presents MainCameraView; a main-actor camera manager owns ARKit camera authorization/providers and publishes AVFoundation display layers. |
| Main patterns | SwiftUI scene composition, Observable state owner, Provider session boundary, Async update consumer, Platform-view adapter |
| Project style | Code-rich sample with 6 scanned Swift file(s) and 381 Swift line(s); resources and generated assets are excluded from those counts. |
Project structure
Source bundle/
└── AccessingTheMainCamera/
├── CameraSessionManager.swift # CameraSessionManager, CameraConfiguration
├── CameraFeed.swift # CameraFeed
├── CameraFrameView.swift # CameraFrameView, SampleBufferPreview
├── AccessingTheMainCameraApp.swift # AccessingTheMainCameraApp
├── MainCameraView.swift # MainCameraView
└── ContentView.swift # ContentView
Structure observations
- The runtime boundary is WindowGroup; 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
flowchart LR
AccessingTheMainCameraApp_1["AccessingTheMainCameraApp"]
WindowGroup_2["WindowGroup"]
MainCameraView_3["MainCameraView"]
CameraSessionManager_4["CameraSessionManager"]
ARKit_CameraFrameProvider___AVFoundation_5["ARKit CameraFrameProvider + AVFoundation"]
AccessingTheMainCameraApp_1 --> WindowGroup_2
WindowGroup_2 --> MainCameraView_3
MainCameraView_3 --> CameraSessionManager_4
CameraSessionManager_4 --> ARKit_CameraFrameProvider___AVFoundation_5
Reference code
AccessingTheMainCamera/AccessingTheMainCameraApp.swift:11 — the app or executable entry declares the outer scene lifecycle.
struct AccessingTheMainCameraApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}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
classDiagram
MainCameraView *-- CameraSessionManager : sessionManager
CameraConfiguration --> ARKitSession : arkitSession
CameraConfiguration --> CameraFrameProvider : cameraFrameProvider
CameraFrameView --> CALayer : preview
Ownership evidence
AccessingTheMainCamera/MainCameraView.swift:11 — representative stored state or the nearest verified lifecycle anchor.
@State private var sessionManager = CameraSessionManager()| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
MainCameraView |
CameraSessionManager as sessionManager |
creates and retains | Only the declaring scope writes |
CameraConfiguration |
ARKitSession as arkitSession |
stores and coordinates | Only the declaring scope writes |
CameraConfiguration |
CameraFrameProvider as cameraFrameProvider |
stores and coordinates | Only the declaring scope writes |
CameraFrameView |
CALayer as preview |
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 |
|---|---|---|
AccessingTheMainCameraApp (AccessingTheMainCamera/AccessingTheMainCameraApp.swift:11) |
Declares app scenes and top-level dependency lifetime. | App |
MainCameraView (AccessingTheMainCamera/MainCameraView.swift:10) |
Presents UI and forwards gestures or lifecycle events. | View |
CameraSessionManager (AccessingTheMainCamera/CameraSessionManager.swift:14) |
Coordinates long-lived framework or cross-view work. | Concrete framework collaborators |
CameraFrameView (AccessingTheMainCamera/CameraFrameView.swift:10) |
Presents UI and forwards gestures or lifecycle events. | UIViewRepresentable |
ContentView (AccessingTheMainCamera/ContentView.swift:10) |
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 accessDenied = false (AccessingTheMainCamera/CameraSessionManager.swift:58) |
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 arkitSession: ARKitSession? (AccessingTheMainCamera/CameraSessionManager.swift:52) |
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
AccessingTheMainCamera/CameraSessionManager.swift:58 — representative visibility boundary.
private(set) var accessDenied = false
static let isSupported = CameraFrameProvider.isSupported
// MARK: - Configuration
/// The configuration for the camera frame format.
var configuration: CameraConfiguration = .default {
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Scene declaration and dependency lifetime | AccessingTheMainCameraApp |
The App/entry boundary determines window, volume, and immersive-space lifetime. |
| Presentation, attachments, and gestures | MainCameraView |
SwiftUI view code forwards user intent and RealityView lifecycle events. |
| Shared feature state and commands | CameraSessionManager |
A role-named owner prevents sibling views from duplicating transitions. |
| Tracking authorization and update streams | AccessingTheMainCamera/CameraSessionManager.swift:52 |
Provider lifetime and async updates remain outside render-only view code. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | AccessingTheMainCamera/AccessingTheMainCameraApp.swift:11 |
Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary. |
| Observable state owner | AccessingTheMainCamera/CameraSessionManager.swift:14 |
Shares feature state across multiple views without moving framework resources into view values. |
| Provider session boundary | AccessingTheMainCamera/CameraSessionManager.swift:120 |
Owns provider lifetime separately from the SwiftUI view tree. |
| Async update consumer | AccessingTheMainCamera/CameraSessionManager.swift:90 |
Consumes provider or event streams in cancellable structured tasks. |
| Platform-view adapter | AccessingTheMainCamera/CameraFrameView.swift:10 |
Wraps an imperative platform view/controller behind a SwiftUI value. |
Naming conventions
- Role suffixes are evidence, not decoration: App:
AccessingTheMainCameraApp; Manager:CameraSessionManager; View:CameraFrameView,ContentView,MainCameraView. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
run,clearCameraFeeds,handleCameraSettingChange,observeCameraAccess,observeCameraFrameUpdates,restart,runCameraFrameProvider,update. - Files generally match their primary type;
Views,Models,Managers,Providers,Components,Systems, andPackagesfolders describe architectural roles where present.
Architecture takeaways
- Treat
AccessingTheMainCameraAppas 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.
- Run ARKit providers for the scene lifetime and consume their asynchronous updates in cancellable tasks; map anchors to entities at the boundary.
Source map
| Source file | Relevant symbols |
|---|---|
AccessingTheMainCamera/CameraSessionManager.swift:14 |
CameraSessionManager, CameraConfiguration |
AccessingTheMainCamera/CameraFeed.swift:12 |
CameraFeed |
AccessingTheMainCamera/CameraFrameView.swift:10 |
CameraFrameView, SampleBufferPreview |
AccessingTheMainCamera/AccessingTheMainCameraApp.swift:11 |
AccessingTheMainCameraApp |
AccessingTheMainCamera/MainCameraView.swift:10 |
MainCameraView |
AccessingTheMainCamera/ContentView.swift:10 |
ContentView |