NetworkManager
A modern, async/await based networking layer for Swift.Overview
NetworkManager provides a clean, protocol-oriented way to handle network requests in iOS applications. It supports Swift's modern concurrency model (async/await), automatic decoding, and SSL pinning.
Features
- Async/Await support (iOS 13+)
- Generic Request Strategy
- Automatic JSON Decoding
- SSL Pinning Support
- File Download Support
Basic Usage
Making a RequestDefine your request strategy and use the manager to fetch data.
Swift
let networkManager = NetworkManager()
let request = YourRequestStrategy()
do {
let (decodedData, response) = try await networkManager.request(request)
// Handle the decoded data (T)
} catch {
// Handle NetworkError
}API Reference
Generic RequestDecodes response data into a Codable object.JSON RequestReturns raw JSON object (Any).SSL Pinned RequestValidates the server trust against a local certificate file (.cer).Download Request
Swift
func request<Request: RequestStrategy, T: Codable>(_ request: Request) async throws -> (T, URLResponse)Swift
func request<Request: RequestStrategy>(_ request: Request) async throws -> (Any, URLResponse)Swift
func sslrequest<Request: RequestStrategy, T: Codable>(_ request: Request, cer: String) async throws -> (T, URLResponse)Swift
func downloadRequest<Request: RequestStrategy>(_ request: Request, delegate: URLSessionDownloadDelegate) async throws -> (URL, URLResponse)Error Handling
The manager throws NetworkError for specific failure cases.
invalidResponseThe server returned an invalid response.
missingCertificateThe local certificate file could not be found.
serverCertificateInvalidThe server certificate is not valid.
certificateMismatchThe server certificate does not match the local certificate.