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

Build a workout app for Apple Watch

At a glance

Item Summary
Purpose Create your own workout app, quickly and easily, with HealthKit and SwiftUI.
App architecture A Swift sample with the source-visible chain MyWorkoutsAppStartViewWorkoutManagerHealthKit APIs.
Main patterns View-controller organization, Delegate or data-source callbacks, SwiftUI environment injection, Publisher-backed observable state
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue.main.async; none alone proves a background thread.
State/event model Source-visible mechanisms: ObservableObject, @Published, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, HealthKit, Foundation, WatchKit, ClockKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── MyWorkouts WatchKit Extension/
│   ├── MyWorkoutsApp.swift
│   ├── Views/
│   │   ├── ElapsedTimeView.swift
│   │   ├── MetricsView.swift
│   │   ├── SessionPagingView.swift
│   │   ├── SummaryView.swift
│   │   ├── ControlsView.swift
│   │   ├── StartView.swift
│   │   └── ActivityRingsView.swift
│   ├── ComplicationController.swift
│   └── Controller/
│       └── WorkoutManager.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── MyWorkouts WatchKit App/
    └── Info.plist

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 14 source declaration(s).

Overall architecture

Reference code

MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:10 — architecture anchor

@main
struct MyWorkoutsApp: App {
    @StateObject private var workoutManager = WorkoutManager()

    @SceneBuilder var body: some Scene {
        WindowGroup {
            NavigationView {
                StartView()
            }
            .sheet(isPresented: $workoutManager.showingSummaryView) {
                SummaryView()
            }
            .environmentObject(workoutManager)
        }
    }
}

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 HealthKit.

Ownership and state

Ownership evidence

MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:12 — stored dependency or nearest verified ownership anchor

@main
struct MyWorkoutsApp: App {
    @StateObject private var workoutManager = WorkoutManager()
    // ...
}
Owner Object or state Relationship Mutation authority
MyWorkoutsApp WorkoutManager (workoutManager) owns wrapper-managed state Owning lexical scope
ElapsedTimeView TimeInterval (elapsedTime) stores or receives App/module collaborators
ElapsedTimeView Bool (showSubseconds) owns value state App/module collaborators
ElapsedTimeView ElapsedTimeFormatter (timeFormatter) 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
Queue scheduling DispatchQueue.main.async The source addresses the main dispatch queue. MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:121

@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

MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:121 — representative execution boundary

        DispatchQueue.main.async {
            // ...
            case HKQuantityType.quantityType(forIdentifier: .heartRate):
            // ...
        }

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 ObservableObject ObservableObject supplies an observation contract. MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:11
State propagation @Published A published property can emit owner-controlled changes. MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:19
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:12
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:8
Source import HealthKit The cited file imports this module; runtime use and architectural role are not inferred. MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:8
Source import WatchKit The cited file imports this module; runtime use and architectural role are not inferred. MyWorkouts WatchKit Extension/Views/SessionPagingView.swift:9
Source import ClockKit The cited file imports this module; runtime use and architectural role are not inferred. MyWorkouts WatchKit Extension/ComplicationController.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

MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:11 — representative type boundary

@main
struct MyWorkoutsApp: App {
    @StateObject private var workoutManager = WorkoutManager()
    // ...
}
Type Responsibility Depends on or conforms to
MyWorkoutsApp Application entry and top-level composition App
ElapsedTimeView User-interface presentation and input forwarding View
MetricsView User-interface presentation and input forwarding View
SessionPagingView User-interface presentation and input forwarding View
SummaryView User-interface presentation and input forwarding View
SummaryMetricView User-interface presentation and input forwarding View
ControlsView User-interface presentation and input forwarding View
StartView User-interface presentation and input forwarding View
ComplicationController View lifecycle, callbacks, and feature coordination NSObject, CLKComplicationDataSource
WorkoutManager Long-lived feature or framework coordination NSObject, ObservableObject

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
workoutManager (MyWorkouts WatchKit Extension/MyWorkoutsApp.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.
timeFormatter (MyWorkouts WatchKit Extension/Views/ElapsedTimeView.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.
selection (MyWorkouts WatchKit Extension/Views/SessionPagingView.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.
displayMetricsView (MyWorkouts WatchKit Extension/Views/SessionPagingView.swift:38) 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.

Reference code

MyWorkouts WatchKit Extension/MyWorkoutsApp.swift:12 — representative boundary

@main
struct MyWorkoutsApp: App {
    @StateObject private var workoutManager = WorkoutManager()
    // ...
}

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 MyWorkoutsApp The source’s App suffix makes this role explicit.
View lifecycle, callbacks, and feature coordination ComplicationController The source’s Controller suffix makes this role explicit.
Long-lived feature or framework coordination WorkoutManager The source’s Manager suffix makes this role explicit.
User-interface presentation and input forwarding ActivityRingsView, ControlsView, ElapsedTimeView, MetricsView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization MyWorkouts WatchKit Extension/ComplicationController.swift:10 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Delegate or data-source callbacks MyWorkouts WatchKit Extension/ComplicationController.swift:10 Callback protocols invert event delivery back into the sample’s owner.
SwiftUI environment injection MyWorkouts WatchKit Extension/Views/ControlsView.swift:11 The environment supplies state or a capability without threading it through every initializer.
Publisher-backed observable state MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift:19 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: MyWorkoutsApp; Controller: ComplicationController; Manager: WorkoutManager; View: ActivityRingsView, ControlsView, ElapsedTimeView, MetricsView, SessionPagingView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: string, entries, displayMetricsView, getComplicationDescriptors, handleSharedComplicationDescriptors, getTimelineEndDate, getPrivacyBehavior, getCurrentTimelineEntry.
  • Files: MyWorkouts WatchKit Extension/MyWorkoutsApp.swift, MyWorkouts WatchKit Extension/Views/ElapsedTimeView.swift, MyWorkouts WatchKit Extension/Views/MetricsView.swift, MyWorkouts WatchKit Extension/Views/SessionPagingView.swift, MyWorkouts WatchKit Extension/Views/SummaryView.swift, MyWorkouts WatchKit Extension/Views/ControlsView.swift.

Architecture takeaways

  • MyWorkoutsApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, HealthKit, WatchKit, ClockKit 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
MyWorkouts WatchKit Extension/MyWorkoutsApp.swift Cited implementation, MyWorkoutsApp, SwiftUI state property wrapper, SwiftUI
MyWorkouts WatchKit Extension/Views/ElapsedTimeView.swift Cited implementation, ElapsedTimeView, ElapsedTimeFormatter, ElapsedTime_Previews
MyWorkouts WatchKit Extension/Views/SessionPagingView.swift Cited implementation, WatchKit, SessionPagingView, Tab, PagingView_Previews
MyWorkouts WatchKit Extension/ComplicationController.swift ComplicationController, Cited implementation, ClockKit
MyWorkouts WatchKit Extension/Views/ControlsView.swift Cited implementation, ControlsView, ControlsView_Previews
MyWorkouts WatchKit Extension/Controller/WorkoutManager.swift Cited implementation, DispatchQueue.main.async, ObservableObject, @Published, HealthKit, Foundation, WorkoutManager
MyWorkouts WatchKit Extension/Views/MetricsView.swift MetricsView, MetricsView_Previews, MetricsTimelineSchedule
MyWorkouts WatchKit Extension/Views/SummaryView.swift SummaryView, SummaryView_Previews, SummaryMetricView
MyWorkouts WatchKit Extension/Views/StartView.swift StartView, StartView_Previews
MyWorkouts WatchKit Extension/Views/ActivityRingsView.swift ActivityRingsView