All skills
johnrogers avatar

/swift-networking

@d62eb71

Use when implementing Network.framework connections (NWConnection, NetworkConnection), debugging connection failures, migrating from sockets/URLSession streams, or handling network transitions. Covers UDP/TCP patterns, structured concurrency networking (iOS 26+), and common anti-patterns.

Use this Skill: https://skilld.dev/gh/johnrogers/claude-swift-engineering/swift-networking

This session only. Nothing lands on disk.

referencesconnection-states.md

≈700 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Connection State Handling

State Machine

setup -> preparing -> waiting <-> ready -> failed/cancelled
State Meaning Action
.preparing DNS, TCP, TLS handshake Show "Connecting..."
.waiting(error) No network, retrying automatically Show "Waiting for network..."
.ready Connected Begin send/receive
.failed(error) Unrecoverable Show error, offer retry
.cancelled cancel() called Clean up

State Handling (iOS 12+)

connection.stateUpdateHandler = { [weak self] state in
    switch state {
    case .preparing:
        self?.updateUI(.connecting)
    case .waiting(let error):
        // DON'T fail here - framework retries when network returns
        self?.updateUI(.waiting)
    case .ready:
        self?.startCommunication()
    case .failed(let error):
        self?.showError(error)
    case .cancelled:
        self?.cleanup()
    @unknown default: break
    }
}

State Handling (iOS 26+)

Task {
    for await state in connection.states {
        switch state {
        case .preparing: print("Connecting...")
        case .waiting(let error): print("Waiting: \(error)")
        case .ready: await startCommunication()
        case .failed(let error): print("Failed: \(error)")
        case .cancelled: print("Cancelled")
        @unknown default: break
        }
    }
}

The Waiting State (Critical)

.waiting means network unavailable but framework retries automatically.

// WRONG - Poor UX
case .waiting:
    showError("Connection failed")

// CORRECT
case .waiting:
    showStatus("Waiting for network...")

Viability Updates

connection.viabilityUpdateHandler = { isViable in
    if !isViable {
        // Don't tear down! May recover when network returns
        showStatus("Connection interrupted...")
    }
}

Better Path Available

connection.betterPathUpdateHandler = { betterPathAvailable in
    if betterPathAvailable {
        migrateToNewConnection()
    }
}

Multipath TCP

let parameters = NWParameters.tcp
parameters.multipathServiceType = .handover  // Seamless WiFi/cellular transition

Common POSIX Errors

Code Meaning
50 Network interface down
54 Connection reset by peer
60 Connection timed out
61 Connection refused (server not listening)
65 Host unreachable

NWPathMonitor

let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
    if path.status == .satisfied {
        if path.usesInterfaceType(.wifi) { print("WiFi") }
        if path.isExpensive { print("Cellular/hotspot") }
    }
}
monitor.start(queue: .main)

Use for global UI updates, not pre-connection checks.

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides technical documentation and code examples for implementing Apple's Network.framework in Swift applications. No security risks or malicious patterns were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    6/6 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d62eb71. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for johnrogers/claude-swift-engineering/swift-networking