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

Monitoring location changes with Core Location

At a glance

Item Summary
Purpose Define boundaries and act on user location updates.
App architecture A Swift sample with the source-visible chain LocationMonitorSampleAppContentViewObservableMonitorModelCoreLocation APIs.
Main patterns Delegate or data-source callbacks, Publisher-backed observable state, Actor-isolated state
Project style 3 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── LocationMonitorSampleApp/
│   ├── LocationMonitorSampleApp.swift
│   ├── ContentView.swift
│   ├── AppDelegate.swift
│   └── Info.plist
├── Configuration/
│   └── SampleCode.xcconfig
└── LocationMonitorSampleApp.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

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 4 project/configuration file(s) and 5 source declaration(s).

Overall architecture

Reference code

LocationMonitorSampleApp/LocationMonitorSampleApp.swift:10 — architecture anchor

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

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 Core Location.

Ownership and state

Ownership evidence

LocationMonitorSampleApp/ContentView.swift:12 — stored dependency or nearest verified ownership anchor

let appleParkLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)
let testBeaconId = UUID(uuidString: "A2C56DB5-DFFB-48D2-B060-D0F5A71096E0")!

let globalAuthDeniedError = "Please enable Location Services by going to Settings -> Privacy & Security"
let authDeniedError = "Please authorize LocationMonitorSampleApp to access Location Services"
let authRestrictedError = "LocationMonitorSampleApp can't access your location. Do you have Parental Controls enabled?"
let accuracyLimitedError = "LocationMonitorSampleApp can't function without access to your precise location"
Owner Object or state Relationship Mutation authority
ContentView CLLocationCoordinate2D (appleParkLocation) creates and retains Initialized by the owner; the binding is immutable
ContentView UUID (testBeaconId) owns value state Initialized by the owner; the binding is immutable
ObservableMonitorModel ObservableMonitorModel (shared) creates and retains Initialized by the owner; the binding is immutable
ObservableMonitorModel CLMonitor (monitor) 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

LocationMonitorSampleApp/LocationMonitorSampleApp.swift:11 — representative type boundary

struct LocationMonitorSampleApp: App {
    // ...
}
Type Responsibility Depends on or conforms to
LocationMonitorSampleApp Application entry and top-level composition App
ObservableMonitorModel Feature data or observable state ObservableObject
ContentView User-interface presentation and input forwarding View
ErrorView User-interface presentation and input forwarding View
AppDelegate Receives callback-driven events NSObject, UIApplicationDelegate

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
monitor (LocationMonitorSampleApp/ContentView.swift:38) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
authSession (LocationMonitorSampleApp/ContentView.swift:48) 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.
notificationCenter (LocationMonitorSampleApp/ContentView.swift:49) 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.
notificationContent (LocationMonitorSampleApp/ContentView.swift:50) 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

LocationMonitorSampleApp/ContentView.swift:38 — representative boundary

@MainActor
public class ObservableMonitorModel: ObservableObject {
    // ...
    public var monitor: CLMonitor?
    // ...
}

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 LocationMonitorSampleApp The source’s App suffix makes this role explicit.
Receives callback-driven events AppDelegate The source’s Delegate suffix makes this role explicit.
Feature data or observable state ObservableMonitorModel The source’s Model suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, ErrorView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Delegate or data-source callbacks LocationMonitorSampleApp/AppDelegate.swift:12 Callback protocols invert event delivery back into the sample’s owner.
Publisher-backed observable state LocationMonitorSampleApp/ContentView.swift:39 Published properties notify observers while mutation remains with the state object.
Actor-isolated state LocationMonitorSampleApp/ContentView.swift:30 Actor annotations make the concurrency ownership boundary explicit.

Main application flow

One launch starts a long-lived monitor task that registers conditions, restores their last events, and then reduces each asynchronous event into observable UI rows.

Reference code

LocationMonitorSampleApp/ContentView.swift:77 — the task registers both conditions before consuming the monitor’s event sequence.

func startMonitoringConditions() {
    Task {
        monitor = await CLMonitor(monitorName)

        await monitor!.add(getCircularGeographicCondition(), identifier: "ApplePark")
        await monitor!.add(getBeaconIdentityCondition(), identifier: "TestBeacon")
        for identifier in await monitor!.identifiers {
            guard let lastEvent = await monitor!.record(for: identifier)?.lastEvent else { continue }
            UIRows[identifier] = [lastEvent]
        }
        for try await event in await monitor!.events {
            guard let lastEvent = await monitor!.record(for: event.identifier)?.lastEvent else { continue }
            if event.state == lastEvent.state { continue }
            UIRows[event.identifier] = [event]
            UIRows[event.identifier]?.append(lastEvent)
        }
    }
}

Naming conventions

  • Types: App: LocationMonitorSampleApp; Delegate: AppDelegate; Model: ObservableMonitorModel; View: ContentView, ErrorView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: without, startAuthSession, startMonitoringConditions, updateRecords, getCircularGeographicCondition, getBeaconIdentityCondition, application.
  • Files: LocationMonitorSampleApp/LocationMonitorSampleApp.swift, LocationMonitorSampleApp/ContentView.swift, LocationMonitorSampleApp/AppDelegate.swift.

Architecture takeaways

  • LocationMonitorSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, CoreLocation, UIKit 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
LocationMonitorSampleApp/LocationMonitorSampleApp.swift LocationMonitorSampleApp
LocationMonitorSampleApp/ContentView.swift ObservableMonitorModel, ContentView, ErrorView, ContentView_Previews
LocationMonitorSampleApp/AppDelegate.swift AppDelegate