iOS Networking
Priority: P0
Implementation Workflow
- Choose networking layer — Use native
URLSessionwith async/await for simple apps;Alamofirefor production APIs with interceptors. - Build URLs safely — Use
URLComponentsandURLQueryItem; never use string interpolation for URL parameters. - Decode with Codable — Use
Codablefor all JSON mapping. Prefersnake_casekey decoding strategies. - Add auth interceptor — Use
RequestInterceptorto injectAuthorization: Bearer <token>on all requests. - Handle token refresh — On 401, use
RequestInterceptor.onRetryto callrefreshToken()and retry. - Pin certificates — Use
ServerTrustManagerorTrustKitfor production-grade security.
See URLSession and Alamofire implementation examples
Anti-Patterns
- ❌ UI updates from background thread — always dispatch to
@MainActoror main queue - ❌ Manual
JSONSerialization— useCodableandJSONDecoder - ❌ No timeout set — always set a reasonable
timeoutInterval(30s default)