Sample CodeDriverKit, iOS, iPadOS, Mac Catalyst, macOSReviewed 2026-07-21View on Apple Developer

Communicating between a DriverKit extension and a client app

At a glance

Item Summary
Purpose Send and receive different kinds of data securely by validating inputs and asynchronously by storing and using a callback.
App architecture A C, C++, C/Objective-C header, Swift sample with the source-visible chain DriverKitSampleAppDriverKitSampleViewDriverCommunicationViewModelDriverKit APIs.
Main patterns Model-View-ViewModel, Delegate or data-source callbacks, Binding-based state propagation, Publisher-backed observable state
Project style 12 scanned source file(s) across C, C++, C/Objective-C header, Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
└── DriverKitUserClientSample/
    ├── DriverKitSampleApp/
    │   ├── DriverKitSampleApp.swift
    │   ├── DriverCommunicationViewModel.swift
    │   ├── DriverCommunicationView.swift
    │   ├── DriverKitSampleView.swift
    │   ├── DriverLoadingViewModel+macOS.swift
    │   ├── DriverLoadingView+iOS.swift
    │   ├── DriverLoadingView+macOS.swift
    │   ├── DriverKitSampleApp-Bridging-Header.h
    │   ├── UserClientCommunication.c
    │   └── UserClientCommunication.h
    └── NullDriver/
        ├── NullDriverUserClient.cpp
        └── NullDriver.cpp

Structure observations

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

Overall architecture

Reference code

DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift:10 — architecture anchor

@main
struct DriverKitSampleApp: App {
    var body: some Scene {
        WindowGroup {
            DriverKitSampleView()
        }
    }
}

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

Ownership and state

Ownership evidence

DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:66 — stored dependency or nearest verified ownership anchor

class DriverCommunicationViewModel: NSObject, ObservableObject {
    // ...
    @Published public var isConnected: Bool = false
    // ...
}
Owner Object or state Relationship Mutation authority
DriverCommunicationViewModel Bool (isConnected) owns value state App/module collaborators
DriverCommunicationViewModel io_connect_t (connection) stores or receives App/module collaborators
DriverCommunicationViewModel UnsafeMutableRawPointer (opaqueSelf) creates and retains App/module collaborators
DriverCommunicationViewModel State (state) stores or receives 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.

Class and protocol design

DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift:11 — representative type boundary

struct DriverKitSampleApp: App {
    var body: some Scene {
        WindowGroup {
            DriverKitSampleView()
        }
    }
}
Type Responsibility Depends on or conforms to
DriverKitSampleApp Application entry and top-level composition App
DriverCommunicationViewModel UI-facing state and feature coordination NSObject, ObservableObject
DriverCommunicationView User-interface presentation and input forwarding View
DriverKitSampleView User-interface presentation and input forwarding View
DisplayedView User-interface presentation and input forwarding Concrete collaborators/imported frameworks
DriverLoadingViewModel UI-facing state and feature coordination NSObject
DriverLoadingView User-interface presentation and input forwarding View
DriverLoadingView User-interface presentation and input forwarding View
DriverCommunicationStateMachine Owns feature behavior and collaborator lifecycle Concrete collaborators/imported frameworks
State Represents mutable feature 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
viewModel (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:13) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
headerView (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:35) 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.
uncheckedView (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:55) 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.
checkedView (DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:87) 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

DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:13 — representative boundary

    @ObservedObject public var viewModel: DriverCommunicationViewModel = .init()

    var body: some View {
        // ...
    }

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 DriverKitSampleApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding DisplayedView, DriverCommunicationView, DriverKitSampleView, DriverLoadingView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination DriverCommunicationViewModel, DriverLoadingViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:64 Role-named view models keep UI-facing state or coordination outside view declarations.
Delegate or data-source callbacks DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift:158 Callback protocols invert event delivery back into the sample’s owner.
Binding-based state propagation DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift:12 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Publisher-backed observable state DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift:66 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: DriverKitSampleApp; View: DisplayedView, DriverCommunicationView, DriverKitSampleView, DriverLoadingView; ViewModel: DriverCommunicationViewModel, DriverLoadingViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: SwiftAsyncCallback, SwiftDeviceAdded, SwiftDeviceRemoved, process, LocalAsyncCallback, ProcessResult, SwiftUncheckedScalar, SwiftUncheckedStruct.
  • Files: DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift, DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift, DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift, DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleView.swift.

Architecture takeaways

  • DriverKitSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches DriverKit, SwiftUI, IOKit, CoreFoundation 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
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp.swift DriverKitSampleApp
DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationViewModel.swift DriverCommunicationStateMachine, State, Event, DriverCommunicationViewModel
DriverKitUserClientSample/DriverKitSampleApp/DriverCommunicationView.swift DriverCommunicationView, DriverCommunicationView_Previews
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleView.swift DriverKitSampleView, DisplayedView
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingViewModel+macOS.swift DriverLoadingStateMachine, State, Event, DriverLoadingViewModel
DriverKitUserClientSample/NullDriver/NullDriverUserClient.cpp NullDriverUserClient_IVars
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingView+iOS.swift DriverLoadingView, DriverLoadingView_Previews
DriverKitUserClientSample/DriverKitSampleApp/DriverLoadingView+macOS.swift DriverLoadingView, DriverLoadingView_Previews
DriverKitUserClientSample/NullDriver/NullDriver.cpp NullDriver_IVars
DriverKitUserClientSample/DriverKitSampleApp/DriverKitSampleApp-Bridging-Header.h Feature implementation