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

Configuring a Wi-Fi accessory to join a network

At a glance

Item Summary
Purpose Associate an iOS device with an accessory’s network to deliver network configuration information.
App architecture A Swift sample bundle with entry-bearing project variants BrandXAccessory, BrandXAccessorySetup, each leading to Network / NetworkExtension APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 25 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue.main.async, DispatchQueue.global; none alone proves a background thread.
State/event model Source-visible mechanisms: NotificationCenter.
Key frameworks/packages Foundation, UIKit, Cocoa, Network, NetworkExtension; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── BrandXAccessory/
│   ├── AppDelegate.swift
│   └── UI/
│       └── ViewControllers/
│           ├── BrandXAccessoryBaseViewController.swift
│           ├── BrandXAccessoryConfigureViewController.swift
│           ├── BrandXAccessoryDoneViewController.swift
│           └── BrandXAccessorySetupViewController.swift
└── BrandXAccessorySetup/
    ├── AppDelegate.swift
    ├── Network/
    │   ├── AccessoryNetworkManager.swift
    │   └── DeviceNetworkManager.swift
    ├── UI/
    │   └── ViewControllers/
    │       ├── QRScanningViewController.swift
    │       └── SetupBaseViewController.swift
    ├── Location/
    │   └── LocationManager.swift
    └── SceneDelegate.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 10 project/configuration file(s) and 30 source declaration(s).

Overall architecture

Reference code

BrandXAccessory/AppDelegate.swift:10 — architecture anchor

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
}

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

BrandXAccessorySetup/Network/AccessoryNetworkManager.swift:17 — stored dependency or nearest verified ownership anchor

final class AccessoryNetworkManager {
    // ...
    public weak var delegate: Delegate?
    // ...
}
Owner Object or state Relationship Mutation authority
AccessoryNetworkManager Delegate (delegate) holds a non-owning reference The referenced object’s lifecycle is owned elsewhere
AccessoryNetworkManager Bool (isReady) owns value state App/module collaborators
AccessoryNetworkManager String (status) owns value state App/module collaborators
QRScanningViewController QRScanningDelegate (delegate) holds a non-owning reference The referenced object’s lifecycle is owned elsewhere

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. BrandXAccessory/UI/ViewControllers/BrandXAccessoryConfigureViewController.swift:53
Queue scheduling DispatchQueue.global The source addresses a global dispatch queue; no stable thread identity is implied. BrandXAccessory/Utility/ConfigurationUtility.swift:57

@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

BrandXAccessory/UI/ViewControllers/BrandXAccessoryConfigureViewController.swift:53 — representative execution boundary

            DispatchQueue.main.async {
                // Notice the prepended WebSocket secure protocol.
                self.urlComponents.host = "\(ipAddr)"
                self.serviceNameValue.stringValue = "\(self.urlComponents.scheme!)://\(self.urlComponents.host!):\(self.urlComponents.port!)"
                self.updateQRCode()
            }

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 NotificationCenter NotificationCenter distributes named process-local events. BrandXAccessorySetup/UI/ViewControllers/SetupConfirmHotspotViewController.swift:25
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. BrandXAccessory/Network/WebSocketListener.swift:8
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. BrandXAccessorySetup/AppDelegate.swift:8
Source import Cocoa The cited file imports this module; runtime use and architectural role are not inferred. BrandXAccessory/AppDelegate.swift:8
Source import Network The cited file imports this module; runtime use and architectural role are not inferred. BrandXAccessory/Network/WebSocketListener.swift:9
Source import NetworkExtension The cited file imports this module; runtime use and architectural role are not inferred. BrandXAccessorySetup/Network/AccessoryNetworkManager.swift:9
Source import CoreWLAN The cited file imports this module; runtime use and architectural role are not inferred. BrandXAccessory/UI/ViewControllers/BrandXAccessoryConfigureViewController.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

BrandXAccessorySetup/Network/AccessoryNetworkManager.swift:54 — representative type boundary

protocol AccessoryNetworkManagerDelegate: AnyObject {
    func accessoryNetworkManagerDidUpdate(acessoryNetworkManager: AccessoryNetworkManager)
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events NSObject, NSApplicationDelegate
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
AccessoryNetworkManagerDelegate Defines a capability or collaboration contract AnyObject
QRScanningDelegate Defines a capability or collaboration contract AnyObject
WebSocketListenerDelegate Defines a capability or collaboration contract AnyObject
WebSocketConnectionDelegate Defines a capability or collaboration contract AnyObject
AccessoryNetworkManager Long-lived feature or framework coordination Concrete collaborators/imported frameworks
QRScanningViewController View lifecycle, callbacks, and feature coordination UIViewController
BrandXAccessoryBaseViewController View lifecycle, callbacks, and feature coordination NSViewController
BrandXAccessoryConfigureViewController View lifecycle, callbacks, and feature coordination BrandXAccessoryBaseViewController

The source explicitly defines local protocol relationships: BrandXAccessoryConfigureViewControllerWebSocketListenerDelegate, BrandXAccessoryConfigureViewControllerWebSocketConnectionDelegate, SetupHotspotConnectViewControllerAccessoryNetworkManagerDelegate, SetupQRCodeViewControllerQRScanningDelegate, SetupSendDataViewControllerWebSocketConnectionDelegate.

Access control

Symbol Access Verified effect Likely rationale
websocketListener (BrandXAccessory/Network/WebSocketListener.swift:15) 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.
didListenerFail (BrandXAccessory/Network/WebSocketListener.swift:16) 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.
didConnectionSetup (BrandXAccessory/Network/WebSocketListener.swift:17) 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.
shared (BrandXAccessory/Network/WebSocketListener.swift:21) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

BrandXAccessory/Network/WebSocketListener.swift:15 — representative boundary

class WebSocketListener {
    // ...
    private var websocketListener: NWListener?
    // ...
}

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
View lifecycle, callbacks, and feature coordination BrandXAccessoryBaseViewController, BrandXAccessoryConfigureViewController, BrandXAccessoryDoneViewController, BrandXAccessorySetupViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AccessoryNetworkManagerDelegate, AppDelegate, QRScanningDelegate, SceneDelegate The source’s Delegate suffix makes this role explicit.
Long-lived feature or framework coordination AccessoryNetworkManager, DeviceNetworkManager, LocationManager The source’s Manager suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization BrandXAccessory/UI/ViewControllers/BrandXAccessoryBaseViewController.swift:10 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction BrandXAccessory/UI/ViewControllers/BrandXAccessoryConfigureViewController.swift:137 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks BrandXAccessory/AppDelegate.swift:11 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: Controller: BrandXAccessoryBaseViewController, BrandXAccessoryConfigureViewController, BrandXAccessoryDoneViewController, BrandXAccessorySetupViewController, QRScanningViewController; Delegate: AccessoryNetworkManagerDelegate, AppDelegate, QRScanningDelegate, SceneDelegate, WebSocketConnectionDelegate; Manager: AccessoryNetworkManager, DeviceNetworkManager, LocationManager.
  • Protocols: AccessoryNetworkManagerDelegate, QRScanningDelegate, WebSocketListenerDelegate, WebSocketConnectionDelegate.
  • Methods: applicationShouldTerminateAfterLastWindowClosed, removeConfiguration, applyConfiguration, accessoryNetworkManagerDidUpdate, viewDidLoad, viewWillAppear, viewWillDisappear, viewDidLayoutSubviews.
  • Files: BrandXAccessory/AppDelegate.swift, BrandXAccessorySetup/AppDelegate.swift, BrandXAccessorySetup/Network/AccessoryNetworkManager.swift, BrandXAccessorySetup/UI/ViewControllers/QRScanningViewController.swift, BrandXAccessory/UI/ViewControllers/BrandXAccessoryBaseViewController.swift, BrandXAccessory/UI/ViewControllers/BrandXAccessoryConfigureViewController.swift.

Architecture takeaways

  • AppDelegate is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches UIKit, Cocoa, Network, NetworkExtension 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
BrandXAccessory/AppDelegate.swift Cited implementation, Cocoa, AppDelegate
BrandXAccessorySetup/Network/AccessoryNetworkManager.swift Cited implementation, AccessoryNetworkManagerDelegate, NetworkExtension, AccessoryNetworkManager
BrandXAccessory/Network/WebSocketListener.swift Cited implementation, Foundation, Network, WebSocketListener, WebSocketListenerDelegate
BrandXAccessory/UI/ViewControllers/BrandXAccessoryBaseViewController.swift BrandXAccessoryBaseViewController
BrandXAccessory/UI/ViewControllers/BrandXAccessoryConfigureViewController.swift Cited implementation, DispatchQueue.main.async, CoreWLAN, BrandXAccessoryConfigureViewController
BrandXAccessory/Utility/ConfigurationUtility.swift DispatchQueue.global, ConfigurationUtility
BrandXAccessorySetup/UI/ViewControllers/SetupConfirmHotspotViewController.swift NotificationCenter, SetupConfirmHotspotViewController
BrandXAccessorySetup/AppDelegate.swift UIKit, AppDelegate
BrandXAccessorySetup/UI/ViewControllers/QRScanningViewController.swift QRScanningViewController, QRScanningDelegate
BrandXAccessory/UI/ViewControllers/BrandXAccessoryDoneViewController.swift BrandXAccessoryDoneViewController
BrandXAccessory/UI/ViewControllers/BrandXAccessorySetupViewController.swift BrandXAccessorySetupViewController
BrandXAccessorySetup/Location/LocationManager.swift LocationManager
BrandXAccessorySetup/Network/DeviceNetworkManager.swift DeviceNetworkManager
BrandXAccessorySetup/SceneDelegate.swift SceneDelegate
BrandXAccessorySetup/UI/ViewControllers/SetupBaseViewController.swift SetupBaseViewController
BrandXAccessorySetup/UI/ViewControllers/SetupHotspotConnectViewController.swift SetupHotspotConnectViewController