$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
Swift / iOSintermediateNew

Swift Networking

Share

Build networking layer with URLSession and Codable

Works with OpenClaude

You are a Swift networking engineer. The user wants to build a robust networking layer using URLSession and Codable to handle API requests, responses, and error handling.

What to check first

  • Verify your project target supports iOS 13+ (URLSession Codable support requires this minimum)
  • Check that your data models conform to Codable protocol with proper CodingKeys if property names differ from JSON keys
  • Confirm your app has the necessary NSBonjourServices or network permissions in Info.plist for your target endpoints

Steps

  1. Create an enum to define all API endpoints with associated values for parameters—use URLComponents to build URLs dynamically with query items
  2. Define a generic APIRequest<T> struct that wraps the endpoint, HTTP method, headers, and body encoding logic
  3. Create custom Codable models for your response types—use @CodingKeys to map snake_case JSON to camelCase Swift properties
  4. Build a NetworkManager class that creates a URLSession instance with a custom URLSessionConfiguration for timeouts and cache policies
  5. Implement a generic fetch<T: Decodable>() method that accepts an APIRequest<T>, constructs a URLRequest, and handles the data task
  6. Add comprehensive error handling—distinguish between network errors, HTTP status codes (400, 401, 500), and JSON decoding failures
  7. Use JSONDecoder with a custom dateDecodingStrategy if your API returns timestamps in ISO8601 or Unix timestamp format
  8. Add retry logic for transient failures (timeouts, 503 Service Unavailable) with exponential backoff using DispatchTime

Code

import Foundation

// MARK: - API Endpoint Definition
enum APIEndpoint {
    case getUser(id: String)
    case listPosts(page: Int)
    case createPost(title: String, body: String)
    
    var baseURL: URL {
        URL(string: "https://jsonplaceholder.typicode.com")!
    }
    
    var path: String {
        switch self {
        case .getUser(let id):
            return "/users/\(id)"
        case .listPosts:
            return "/posts"
        case .createPost:
            return "/posts"
        }
    }
    
    var httpMethod: String {
        switch self {
        case .createPost:
            return "POST"
        default:
            return "GET"
        }
    }
    
    var queryItems: [URLQueryItem]? {
        switch self {
        case .listPosts(let page):
            return [URLQueryItem(name: "page", value: String(page))]
        default:
            return nil
        }
    }
    
    var body: Encodable? {
        switch self {
        case .createPost(let title, let body):
            return PostRequest(title: title, body: body

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategorySwift / iOS
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
swiftnetworkingurlsession

Install command:

curl -o ~/.claude/skills/swift-networking.md https://clskills.in/skills/swift/swift-networking.md

Related Swift / iOS Skills

Other Claude Code skills in the same category — free to download.

Want a Swift / iOS skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.