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

Building a workout app for iPhone and iPad

At a glance

Item Summary
Purpose Start a workout in iOS, control it from the Lock Screen with App Intents, and present the workout status with Live Activities.
App architecture A Swift sample bundle with entry-bearing project variants WorkoutWidget, WorkoutsOniOSSampleApp, each leading to HealthKit APIs.
Main patterns Model-View-ViewModel, Delegate or data-source callbacks, Actor-isolated state
Project style 22 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── WorkoutsOniOSSampleApp/
│   ├── WorkoutsOniOSSampleApp.swift
│   ├── WorkoutsOniOSSampleAppDelegate.swift
│   ├── Views/
│   │   ├── MetricsView.swift
│   │   └── SummaryView.swift
│   ├── NavigationModel.swift
│   ├── WorkoutManager.swift
│   └── CountDownManager.swift
├── WorkoutWidget/
│   ├── WorkoutWidgetBundle.swift
│   ├── WorkoutWidgetViewModel.swift
│   └── LiveActivityView.swift
└── Shared/
    ├── ElapsedTimeView.swift
    └── MetricsModel.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 6 project/configuration file(s) and 29 source declaration(s).

Overall architecture

Reference code

WorkoutsOniOSSampleApp/WorkoutsOniOSSampleApp.swift:10 — architecture anchor

@main
struct WorkoutsOniOSSampleApp: App {
    // ...
}

Interpretation

The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.

Ownership and state

Ownership evidence

WorkoutsOniOSSampleApp/WorkoutsOniOSSampleApp.swift:14 — stored dependency or nearest verified ownership anchor

    @UIApplicationDelegateAdaptor(WorkoutsOniOSSampleAppDelegate.self) var appDelegate
    @State private var workoutManager = WorkoutManager.shared
    @State private var navigationModel = NavigationModel()
Owner Object or state Relationship Mutation authority
WorkoutsOniOSSampleApp NavigationModel (navigationModel) owns wrapper-managed state Owning lexical scope
WorkoutWidgetViewModel WorkoutWidgetViewModel (shared) creates and retains Initialized by the owner; the binding is immutable
ActivityViewState ActivityState (activityState) stores or receives App/module collaborators
ActivityViewState ContentState (contentState) stores or receives App/module collaborators

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.

Class and protocol design

WorkoutsOniOSSampleApp/WorkoutsOniOSSampleApp.swift:11 — representative type boundary

struct WorkoutsOniOSSampleApp: App {
    // ...
}
Type Responsibility Depends on or conforms to
WorkoutsOniOSSampleApp Application entry and top-level composition App
WorkoutWidgetViewModel UI-facing state and feature coordination Concrete collaborators/imported frameworks
WorkoutsOniOSSampleAppDelegate Receives callback-driven events NSObject, UIApplicationDelegate
MetricsView User-interface presentation and input forwarding View
ElapsedTimeView User-interface presentation and input forwarding View
NavigationModel Feature data or observable state Concrete collaborators/imported frameworks
SummaryView User-interface presentation and input forwarding View
SummaryMetricView User-interface presentation and input forwarding View
WorkoutManager Long-lived feature or framework coordination NSObject
MetricsModel Feature data or observable state Concrete collaborators/imported frameworks

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
timeFormatter (Shared/ElapsedTimeView.swift:12) 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.
preview (WorkoutWidget/WorkoutWidgetLiveActivity.swift:42) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.
live (WorkoutWidget/WorkoutWidgetLiveActivity.swift:48) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.
stale (WorkoutWidget/WorkoutWidgetLiveActivity.swift:59) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.

Reference code

Shared/ElapsedTimeView.swift:12 — representative boundary

    @State private var timeFormatter = ElapsedTimeFormatter()

    var body: some View {
        Text(NSNumber(value: elapsedTime), formatter: timeFormatter)
            .fontWeight(.semibold)
    }

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 WorkoutsOniOSSampleApp The source’s App suffix makes this role explicit.
Receives callback-driven events WorkoutsOniOSSampleAppDelegate, WorkoutsOniOSSampleAppSceneDelegate The source’s Delegate suffix makes this role explicit.
Handles callbacks or feature events IntentHandler The source’s Handler suffix makes this role explicit.
Long-lived feature or framework coordination CountDownManager, WorkoutManager The source’s Manager suffix makes this role explicit.
Feature data or observable state MetricsModel, NavigationModel The source’s Model suffix makes this role explicit.
User-interface presentation and input forwarding ControlsView, CountDownView, ElapsedTimeView, LiveActivityView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination WorkoutWidgetViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel WorkoutWidget/WorkoutWidgetViewModel.swift:12 Role-named view models keep UI-facing state or coordination outside view declarations.
Delegate or data-source callbacks WorkoutsOniOSSampleApp/WorkoutsOniOSSampleAppDelegate.swift:13 Callback protocols invert event delivery back into the sample’s owner.
Actor-isolated state WorkoutWidget/WorkoutWidgetViewModel.swift:12 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: WorkoutsOniOSSampleApp; Delegate: WorkoutsOniOSSampleAppDelegate, WorkoutsOniOSSampleAppSceneDelegate; Handler: IntentHandler; Manager: CountDownManager, WorkoutManager; Model: MetricsModel, NavigationModel; View: ControlsView, CountDownView, ElapsedTimeView, LiveActivityView, MetricsView; ViewModel: WorkoutWidgetViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: startLiveActivity, updateLiveActivity, endLiveActivity, endActivity, setup, observeActivity, updateWorkoutLiveActivity, cleanUpDismissedActivity.
  • Files: WorkoutsOniOSSampleApp/WorkoutsOniOSSampleApp.swift, WorkoutWidget/WorkoutWidgetBundle.swift, WorkoutWidget/WorkoutWidgetViewModel.swift, WorkoutsOniOSSampleApp/WorkoutsOniOSSampleAppDelegate.swift, WorkoutsOniOSSampleApp/Views/MetricsView.swift, Shared/ElapsedTimeView.swift.

Architecture takeaways

  • WorkoutsOniOSSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, HealthKit, ActivityKit, WidgetKit 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.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
WorkoutsOniOSSampleApp/WorkoutsOniOSSampleApp.swift WorkoutsOniOSSampleApp
WorkoutWidget/WorkoutWidgetBundle.swift WorkoutWidgetBundle
WorkoutWidget/WorkoutWidgetViewModel.swift WorkoutWidgetViewModel, ActivityViewState
WorkoutsOniOSSampleApp/WorkoutsOniOSSampleAppDelegate.swift WorkoutsOniOSSampleAppDelegate
WorkoutsOniOSSampleApp/Views/MetricsView.swift MetricsView, CustomAlignment, MetricsTimelineSchedule
Shared/ElapsedTimeView.swift ElapsedTimeView, ElapsedTimeFormatter
WorkoutsOniOSSampleApp/NavigationModel.swift NavigationModel, NavigationState
WorkoutsOniOSSampleApp/Views/SummaryView.swift SummaryView, SummaryMetricView
WorkoutsOniOSSampleApp/WorkoutManager.swift WorkoutManager, SessionSateChange
Shared/MetricsModel.swift MetricsModel