Network.framework Networking
When to Use This Skill
Use when:
- Implementing UDP/TCP connections for gaming, streaming, or messaging apps
- Migrating from BSD sockets, CFSocket, NSStream, or SCNetworkReachability
- Debugging connection timeouts or TLS handshake failures
- Supporting network transitions (WiFi β cellular) gracefully
- Adopting structured concurrency networking patterns (iOS 26+)
- Implementing custom protocols over TLS/QUIC
- Requesting code review of networking implementation before shipping
Related Skills
- Use
axiom-networking-diag for systematic troubleshooting of connection failures, timeouts, and performance issues
- Use
axiom-network-framework-ref for comprehensive API reference with all WWDC examples
Example Prompts
1. "How do I migrate from SCNetworkReachability? My app checks connectivity before connecting."
2. "My connection times out after 60 seconds. How do I debug this?"
3. "Should I use NWConnection or NetworkConnection? What's the difference?"
Red Flags β Anti-Patterns to Prevent
If you're doing ANY of these, STOP and use the patterns in this skill:
β CRITICAL β Never Do These
1. Using SCNetworkReachability to check connectivity before connecting
if SCNetworkReachabilityGetFlags(reachability, &flags) {
connection.start()
}
Why this fails Network state changes between reachability check and connect(). You miss Network.framework's smart connection establishment (Happy Eyeballs, proxy handling, WiFi Assist). Apple deprecated this API in 2018.
2. Blocking socket operations on main thread
let socket = socket(AF_INET, SOCK_STREAM, 0)
connect(socket, &addr, addrlen)
Why this fails Main thread hang β frozen UI β App Store rejection for responsiveness. Even "quick" connects take 200-500ms.
3. Manual DNS resolution with getaddrinfo
var hints = addrinfo(...)
getaddrinfo("example.com", "443", &hints, &results)
Why this fails You reimplement 10+ years of Apple's connection logic poorly. Misses IPv4/IPv6 racing, proxy evaluation, VPN detection.
4. Hardcoded IP addresses instead of hostnames
let host = "192.168.1.1"
Why this fails Proxy auto-configuration (PAC) needs hostname to evaluate rules. VPNs can't route properly. DNS-based load balancing broken.
5. Ignoring waiting state β not handling lack of connectivity
connection.stateUpdateHandler = { state in
if case .ready = state {
}
}
Why this fails User sees "Connection failed" in Airplane Mode instead of "Waiting for network." No automatic retry when WiFi returns.
6. Not using [weak self] in NWConnection completion handlers
connection.send(content: data, completion: .contentProcessed { error in
self.handleSend(error)
})
Why this fails Connection retains completion handler, handler captures self strongly, self retains connection β memory leak.
7. Mixing async/await and completion handlers in NetworkConnection (iOS 26+)
Task {
let connection = NetworkConnection(...)
connection.send(data)
connection.stateUpdateHandler = { ... }
}
Why this fails NetworkConnection designed for pure async/await. Mixing paradigms creates difficult error propagation and cancellation issues.
8. Not supporting network transitions
Why this fails Modern apps must handle network changes gracefully. 40% of connection failures happen during network transitions.
Mandatory First Steps
ALWAYS complete these steps before writing any networking code:
URLSession.shared.dataTask(with: url) { ... }
if #available(iOS 26, *) {
} else {
}
What this tells you
- If HTTP/HTTPS: Use URLSession, not Network.framework
- If iOS 26+ deployment: Use NetworkConnection with async/await
- If iOS 12-25 support needed: Use NWConnection with completion handlers
- If any deprecated API found: Must migrate before shipping (App Store review concern)
Decision Tree
Use this to select the correct pattern in 2 minutes:
Need networking?
ββ HTTP, HTTPS, or WebSocket?
β ββ YES β Use URLSession (NOT Network.framework)
β β
URLSession.shared.dataTask(with: url)
β β
URLSession.webSocketTask(with: url)
β β
URLSession.streamTask(withHostName:port:) for TCP/TLS
β
ββ iOS 26+ and can use structured concurrency?
β ββ YES β NetworkConnection path (async/await)
β ββ TCP with TLS security?
β β ββ Pattern 1a: NetworkConnection + TLS
β β Time: 10-15 minutes
β β
β ββ UDP for gaming/streaming?
β β ββ Pattern 1b: NetworkConnection + UDP
β β Time: 10-15 minutes
β β
β ββ Need message boundaries (framing)?
β β ββ Pattern 1c: TLV Framing
β β Type-Length-Value for mixed message types
β β Time: 15-20 minutes
β β
β ββ Send/receive Codable objects directly?
β ββ Pattern 1d: Coder Protocol
β No manual JSON encoding needed
β Time: 10-15 minutes
β
ββ iOS 12-25 or need completion handlers?
ββ YES β NWConnection path (callbacks)
ββ TCP with TLS security?
β ββ Pattern 2a: NWConnection + TLS
β stateUpdateHandler, completion-based send/receive
β Time: 15-20 minutes
β
ββ UDP streaming with batching?
β ββ Pattern 2b: NWConnection + UDP Batch
β connection.batch for 30% CPU reduction
β Time: 10-15 minutes
β
ββ Listening for incoming connections?
β ββ Pattern 2c: NWListener
β Accept inbound connections, newConnectionHandler
β Time: 20-25 minutes
β
ββ Network discovery (Bonjour)?
ββ Pattern 2d: NWBrowser
Discover services on local network
Time: 25-30 minutes
Quick selection guide
- Gaming (low latency, some loss OK) β UDP patterns (1b or 2b)
- Messaging (reliable, ordered) β TLS patterns (1a or 2a)
- Mixed message types β TLV or Coder (1c or 1d)
- Peer-to-peer β Discovery patterns (2d) + incoming (2c)
Common Patterns
Pattern 1a: NetworkConnection with TLS (iOS 26+)
Use when iOS 26+ deployment, need reliable TCP with TLS security, want async/await
Time cost 10-15 minutes
β BAD: Manual DNS, Blocking Socket
var hints = addrinfo(...)
getaddrinfo("www.example.com", "1029", &hints, &results)
let sock = socket(AF_INET, SOCK_STREAM, 0)
connect(sock, results.pointee.ai_addr, results.pointee.ai_addrlen)
β
GOOD: NetworkConnection with Declarative Stack
import Network
let connection = NetworkConnection(
to: .hostPort(host: "www.example.com", port: 1029)
) {
TLS()
}
public func sendAndReceiveWithTLS() async throws {
let outgoingData = Data("Hello, world!".utf8)
try await connection.send(outgoingData)
let incomingData = try await connection.receive(exactly: 98)