$npx -y skills add johnrogers/claude-swift-engineering --skill swift-networkingUse 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-
| 1 | # Swift Networking |
| 2 | |
| 3 | Network.framework is Apple's modern networking API for TCP/UDP connections, replacing BSD sockets with smart connection establishment, user-space networking, and seamless mobility handling. |
| 4 | |
| 5 | ## Reference Loading Guide |
| 6 | |
| 7 | **ALWAYS load reference files if there is even a small chance the content may be required.** It's better to have the context than to miss a pattern or make a mistake. |
| 8 | |
| 9 | | Reference | Load When | |
| 10 | |-----------|-----------| |
| 11 | | **[Getting Started](references/getting-started.md)** | Setting up NWConnection for TCP/UDP, choosing between APIs | |
| 12 | | **[Connection States](references/connection-states.md)** | Handling `.waiting`, `.ready`, `.failed` transitions | |
| 13 | | **[iOS 26+ Networking](references/ios26-networking.md)** | Using NetworkConnection with async/await, TLV framing, Coder protocol | |
| 14 | | **[Migration Guide](references/migration.md)** | Moving from sockets, CFSocket, SCNetworkReachability, URLSession | |
| 15 | | **[Troubleshooting](references/troubleshooting.md)** | Debugging timeouts, TLS failures, connection issues | |
| 16 | |
| 17 | ## Core Workflow |
| 18 | |
| 19 | 1. Choose transport (TCP/UDP/QUIC) based on use case |
| 20 | 2. Create NWConnection (iOS 12+) or NetworkConnection (iOS 26+) |
| 21 | 3. Set up state handler for connection lifecycle |
| 22 | 4. Start connection on appropriate queue |
| 23 | 5. Send/receive data with proper error handling |
| 24 | 6. Handle network transitions (WiFi to cellular) |
| 25 | |
| 26 | ## When to Use Network.framework vs URLSession |
| 27 | |
| 28 | - **URLSession**: HTTP, HTTPS, WebSocket, simple TCP/TLS streams |
| 29 | - **Network.framework**: UDP, custom protocols, low-level control, peer-to-peer, gaming |
| 30 | |
| 31 | ## Common Mistakes |
| 32 | |
| 33 | 1. **Ignoring state handlers** — Creating an NWConnection without a state change handler means you never learn when it's ready or failed. Always implement the state handler first. |
| 34 | |
| 35 | 2. **Blocking the main thread** — Never call `receive()` on the main queue. Use a background DispatchQueue or Task for all network operations. |
| 36 | |
| 37 | 3. **Wrong queue selection** — Using the wrong queue (UI queue for network work, or serial queue for concurrent reads) causes deadlocks or silent failures. Always explicit your queue choice. |
| 38 | |
| 39 | 4. **Not handling network transitions** — WiFi/cellular switches or network loss aren't always detected automatically. Implement viability checks and state monitoring for robust apps. |
| 40 | |
| 41 | 5. **Improper error recovery** — Network errors need retry logic with backoff. Immediately failing on transient errors (timeouts, temporary loss) creates poor UX. |