NetworkManager is a lightweight, extensible Swift package designed to simplify building RESTful API calls in iOS and SwiftUI apps. It promotes clean, type-safe, and testable networking using Swift's modern concurrency and protocol-oriented programming.
- ✅ Protocol-based abstraction with
Requestable
- 🔁 Built-in retry logic for transient failures
- 🧼 Clean URL construction using
URLComponents
- 🔒 No internet connection detection
- 📦 Works seamlessly with
Codable
request/response bodies - 🔌 Pluggable
URLSession
for testing/mocking
Enum defining standard HTTP methods: .get
, .post
, .put
, .patch
, .delete
.
Defines the base URL and optional API version for a network environment.
protocol NetworkConfiguration {
static var baseURL: URL? { get }
static var apiVersion: String? { get }
}
A generic protocol for defining and executing API requests with typed responses.
protocol Requestable: NetworkConfiguration {
associatedtype Response
var urlPath: String { get }
var httpMethod: HTTPMethod { get }
var requestBody: Codable? { get }
var queryParameters: [String: String]? { get }
var headers: [String: String]? { get }
func request(using session: URLSession, maxRetries: Int, retryDelay: TimeInterval) async throws -> Response
}
Includes built-in:
- Default headers, body, and query parameters
- URL building logic
- Retry mechanism
- Internet connectivity error detection
Defines error types
public enum NetworkError: Error {
case invalidURL
case dataTaskFailed
case httpError(code: Int, data: Data?)
case decodingFailed(Error)
case noInternetConnection
case unknown(Error)
}
See more details: https://github.com/khoavd-dev/NetworkManagerExample
- Swift 5.0+
- iOS 15.0+
- Async/Await based (iOS 15+ recommended for full concurrency)
Built by Khoa Vo. Contributions welcome!