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.

referencesmigration.md

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

Migration Guide

API Mapping

Legacy API Network.framework
socket() + connect() NWConnection
send() / recv() connection.send() / receive()
bind() + listen() NWListener
getaddrinfo() Use hostname in NWEndpoint.Host()
SCNetworkReachability .waiting state
CFSocket / NSStream NWConnection
NSNetService NWBrowser

From BSD Sockets

// WRONG - Blocks main thread
int sock = socket(AF_INET, SOCK_STREAM, 0);
connect(sock, &addr, addrlen);  // BLOCKS
// CORRECT
let connection = NWConnection(host: "example.com", port: 443, using: .tls)
connection.stateUpdateHandler = { [weak self] state in
    if case .ready = state { self?.sendData() }
}
connection.start(queue: .main)  // Non-blocking

From SCNetworkReachability

// WRONG - Race condition
if SCNetworkReachabilityGetFlags(reachability, &flags) {
    if flags.contains(.reachable) {
        connection.start()  // Network may change!
    }
}

// CORRECT - Use waiting state
connection.stateUpdateHandler = { state in
    switch state {
    case .waiting: showStatus("Waiting for network...")
    case .ready: startCommunication()
    case .failed: showError("Failed")
    default: break
    }
}

From getaddrinfo

// WRONG - Misses Happy Eyeballs, proxies
getaddrinfo("example.com", "443", &hints, &results);
// CORRECT - Framework handles DNS, IPv4/IPv6 racing
let connection = NWConnection(
    host: NWEndpoint.Host("example.com"),  // Hostname, not IP
    port: 443, using: .tls
)

From URLSession StreamTask

// Before
let task = URLSession.shared.streamTask(withHostName: "example.com", port: 443)
task.write(data, timeout: 10) { _ in }

// After (iOS 26+)
let connection = NetworkConnection(to: .hostPort(host: "example.com", port: 443)) { TLS() }
try await connection.send(data)

From NWConnection to NetworkConnection

NWConnection NetworkConnection
stateUpdateHandler = {} for await state in states
send(content:completion:) try await send()
receive(min:max:completion:) try await receive()
[weak self] everywhere No weak self needed
Manual JSON Coder(Type.self, using: .json)
// Before
connection.stateUpdateHandler = { [weak self] state in
    if case .ready = state { self?.sendData() }
}

// After
Task {
    for await state in connection.states {
        if case .ready = state {
            try await connection.send(data)
        }
    }
}

Migration Checklist

  • Removed SCNetworkReachability
  • Removed getaddrinfo / manual DNS
  • Removed CFSocket / NSStream
  • Using hostnames, not IPs
  • Handling .waiting state
  • Using [weak self] in NWConnection handlers
  • Tested on real device
  • Tested WiFi/cellular transitions

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