Building peer-to-peer apps
At a glance
| Item | Summary |
|---|---|
| Purpose | Communicate with nearby devices over a secure, high-throughput, low-latency connection by using Wi-Fi Aware. |
| App architecture | A Swift sample with the source-visible chain Wi-FiAwareSampleApp → ContentView → ConnectionManager → SimulationEngine → WiFiAware APIs. |
| Main patterns | Delegate or data-source callbacks, Binding-based state propagation, Actor isolation |
| Project style | 13 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: actor, Sendable or @Sendable, Task, await suspension point, @MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, @Observable. |
| Key frameworks/packages | WiFiAware, Network, OSLog, SwiftUI, Foundation; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── Wi-Fi Aware Sample/
├── Wi-FiAwareSampleApp.swift
├── Simulation/
│ ├── SimulationEngine.swift
│ ├── SimulationView.swift
│ └── SimulationScene.swift
├── ContentView.swift
├── Networking/
│ ├── ConnectionManager.swift
│ ├── DeviceDiscoveryPairingView.swift
│ ├── NetworkManager.swift
│ └── WiFiAwareError.swift
├── PairedDevices/
│ └── PairedDevicesView.swift
└── Extensions/
├── SimulationEngine+Extensions.swift
└── WiFiAware+Extensions.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 5 project/configuration file(s) and 25 source declaration(s).
Overall architecture
flowchart LR
N1["Wi-FiAwareSampleApp"]
N2["ContentView"]
N3["ConnectionManager"]
N4["SimulationEngine"]
N5["WiFiAware APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
Wi-Fi Aware Sample/Wi-FiAwareSampleApp.swift:14 — architecture anchor
@main
struct WiFiAwareSampleApp: App {
var body: some Scene {
WindowGroup {
if WACapabilities.supportedFeatures.contains(.wifiAware) {
ContentView()
} else {
ContentUnavailableView {
Label("This device does not support Wi-Fi Aware", systemImage: "exclamationmark.octagon")
}
}
}
}
}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 Wi-Fi Aware.
Ownership and state
classDiagram
Wi_FiAwareSampleApp *-- Logger : logger
SimulationEngine o-- Mode : mode
SimulationEngine o-- SimulationScene : scene
SimulationEngine o-- NetworkState : networkState
Ownership evidence
Wi-Fi Aware Sample/Wi-FiAwareSampleApp.swift:12 — stored dependency or nearest verified ownership anchor
let logger = Logger(subsystem: "com.example.apple-samplecode.Wi-FiAwareSample", category: "App")| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
Wi-FiAwareSampleApp |
Logger (logger) |
creates and retains | Initialized by the owner; the binding is immutable |
SimulationEngine |
Mode (mode) |
stores or receives | Initialized by the owner; the binding is immutable |
SimulationEngine |
SimulationScene (scene) |
stores or receives | Owning lexical scope |
SimulationEngine |
NetworkState (networkState) |
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.
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 |
|---|---|---|---|
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | Wi-Fi Aware Sample/Networking/ConnectionManager.swift:23 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | Wi-Fi Aware Sample/Networking/ConnectionManager.swift:23 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | Wi-Fi Aware Sample/Networking/ConnectionManager.swift:67 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Wi-Fi Aware Sample/Networking/ConnectionManager.swift:76 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | Wi-Fi Aware Sample/Simulation/SimulationEngine.swift:16 |
@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
Wi-Fi Aware Sample/Networking/ConnectionManager.swift:23 — representative execution boundary
actor ConnectionManager: Sendable {
private var connections: [WiFiAwareConnectionID: WiFiAwareConnection] = [:]
// ...
}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 | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | Wi-Fi Aware Sample/ContentView.swift:12 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | Wi-Fi Aware Sample/Simulation/SimulationEngine.swift:16 |
| Source import | WiFiAware |
The cited file imports this module; runtime use and architectural role are not inferred. | Wi-Fi Aware Sample/Extensions/SimulationEngine+Extensions.swift:8 |
| Source import | Network |
The cited file imports this module; runtime use and architectural role are not inferred. | Wi-Fi Aware Sample/Extensions/SimulationEngine+Extensions.swift:9 |
| Source import | OSLog |
The cited file imports this module; runtime use and architectural role are not inferred. | Wi-Fi Aware Sample/ContentView.swift:9 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Wi-Fi Aware Sample/ContentView.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Wi-Fi Aware Sample/Networking/ConnectionManager.swift:8 |
| Source import | SpriteKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Wi-Fi Aware Sample/Simulation/SimulationEngine.swift:10 |
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
Wi-Fi Aware Sample/Wi-FiAwareSampleApp.swift:15 — representative type boundary
@main
struct WiFiAwareSampleApp: App {
// ...
ContentView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
WiFiAwareSampleApp |
Application entry and top-level composition | App |
SimulationEngine |
Owns processing or simulation work | Concrete collaborators/imported frameworks |
SimulationView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
ConnectionManager |
Long-lived feature or framework coordination | Sendable |
DeviceDiscoveryPairingView |
User-interface presentation and input forwarding | View |
NetworkManager |
Long-lived feature or framework coordination | Concrete collaborators/imported frameworks |
PairedDevicesView |
User-interface presentation and input forwarding | View |
SimulationScene |
Scene lifecycle or scene-level composition | SKScene, SKPhysicsContactDelegate |
ConnectionDetail |
Represents a feature value or composable behavior | Sendable, Equatable |
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 |
|---|---|---|---|
simulationService (Wi-Fi Aware Sample/Extensions/WiFiAware+Extensions.swift:14) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
simulationService (Wi-Fi Aware Sample/Extensions/WiFiAware+Extensions.swift:20) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
connections (Wi-Fi Aware Sample/Networking/ConnectionManager.swift:24) |
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. |
connectionsInfo (Wi-Fi Aware Sample/Networking/ConnectionManager.swift:25) |
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
Wi-Fi Aware Sample/Extensions/WiFiAware+Extensions.swift:14 — representative boundary
public static var simulationService: WAPublishableService {
allServices[simulationServiceName]!
}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 | WiFiAwareSampleApp |
The source’s App suffix makes this role explicit. |
| Owns processing or simulation work | SimulationEngine |
The source’s Engine suffix makes this role explicit. |
| Long-lived feature or framework coordination | ConnectionManager, NetworkManager |
The source’s Manager suffix makes this role explicit. |
| Scene lifecycle or scene-level composition | SimulationScene |
The source’s Scene suffix makes this role explicit. |
| User-interface presentation and input forwarding | ContentView, DeviceDiscoveryPairingView, PairedDevicesView, SimulationView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | Wi-Fi Aware Sample/Simulation/SimulationScene.swift:12 |
Callback protocols invert event delivery back into the sample’s owner. |
| Binding-based state propagation | Wi-Fi Aware Sample/ContentView.swift:34 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Actor isolation | Wi-Fi Aware Sample/Networking/ConnectionManager.swift:23 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Naming conventions
- Types: App: WiFiAwareSampleApp; Engine: SimulationEngine; Manager: ConnectionManager, NetworkManager; Scene: SimulationScene; View: ContentView, DeviceDiscoveryPairingView, PairedDevicesView, SimulationView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
setupEventHandler,startConnectionMonitor,setup,handleLocalEvent,handleConnectionEvent,handleNetworkEvent,run,stopConnection. - Files:
Wi-Fi Aware Sample/Simulation/SimulationEngine.swift,Wi-Fi Aware Sample/Simulation/SimulationView.swift,Wi-Fi Aware Sample/ContentView.swift,Wi-Fi Aware Sample/Networking/ConnectionManager.swift,Wi-Fi Aware Sample/Networking/DeviceDiscoveryPairingView.swift,Wi-Fi Aware Sample/Networking/NetworkManager.swift.
Architecture takeaways
Wi-FiAwareSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches WiFiAware, Network, SwiftUI, SpriteKit 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 |
|---|---|
Wi-Fi Aware Sample/Wi-FiAwareSampleApp.swift |
Cited implementation, WiFiAwareSampleApp |
Wi-Fi Aware Sample/Extensions/WiFiAware+Extensions.swift |
Cited implementation, Feature implementation |
Wi-Fi Aware Sample/Networking/ConnectionManager.swift |
Cited implementation, ConnectionManager, actor, Sendable or @Sendable, Task, await suspension point, Foundation, ConnectionInfo |
Wi-Fi Aware Sample/Simulation/SimulationScene.swift |
Cited implementation, SimulationScene |
Wi-Fi Aware Sample/ContentView.swift |
Cited implementation, SwiftUI state property wrapper, OSLog, SwiftUI, ContentView, MenuButton |
Wi-Fi Aware Sample/Simulation/SimulationEngine.swift |
@MainActor, @Observable, SpriteKit, SimulationEngine, ConnectionDetail, LocalEvent, ConnectionEvent |
Wi-Fi Aware Sample/Extensions/SimulationEngine+Extensions.swift |
WiFiAware, Network, Mode, HostState, ViewerState, NetworkState |
Wi-Fi Aware Sample/Simulation/SimulationView.swift |
SimulationView, OverlayButtonViewModifier, GlaffEffectViewModifier |
Wi-Fi Aware Sample/Networking/DeviceDiscoveryPairingView.swift |
DeviceDiscoveryPairingView, AddDeviceButton |
Wi-Fi Aware Sample/Networking/NetworkManager.swift |
NetworkManager, NetworkEvent |
Wi-Fi Aware Sample/PairedDevices/PairedDevicesView.swift |
PairedDevicesView, DeviceConnectionInfo |
Wi-Fi Aware Sample/Networking/WiFiAwareError.swift |
WiFiAwareError, Category |
Wi-Fi Aware Sample/Networking/NetworkConfig.swift |
Feature implementation |