XMNetworking
XMNetworking is a powerful networking library for iOS written in Swift. It provides a set of efficient tools and abstractions for making network requests, handling responses, and managing network-related tasks.
Features
- Easy-to-use API for making network requests
- Support for various request methods including GET, POST, PUT, DELETE, etc.
- Customizable request headers and parameters
- Automatic JSON serialization and deserialization
- Support for Codable models
- Efficient handling of network responses
- Support for response caching
- HTTP/HTTPS support
- Extensive error handling
- Asynchronous and synchronous request execution
- Ability to track and cancel ongoing network requests
Installation
To integrate XMNetworking into your iOS project, you can use CocoaPods or Swift Package Manager (SPM).
CocoaPods
1. Add the following line to your Podfile:
pod 'XMNetworking'
2. Run the command:
pod install
Swift Package Manager (SPM)
Add XMNetworking to your Xcode project using SPM by following these steps:
- In Xcode, go to File > Swift Packages > Add Package Dependency
- Enter the repository URL: https://github.com/{USERNAME_OR_ORGANIZATION}/XMNetworking.git
- Choose the desired version or the main branch
- Click Next
- Add the package to your desired target
- Click Finish
Usage
To start using XMNetworking in your project:
- Import the XMNetworking module
- Create an instance of XMNetworkingProvider
- Make network requests using the available methods (GET, POST, PUT, DELETE, etc.)
import XMNetworking
let networkingProvider = XMNetworkingProvider(baseUrl: "https://api.example.com")
Available Methods
XMNetworking provides the following methods for making network requests:
get(path:headers:parameters:completion:)
– Makes a GET request to the specified path with optional headers and parameterspost(path:headers:parameters:completion:)
– Makes a POST request to the specified path with optional headers and parametersput(path:headers:parameters:completion:)
– Makes a PUT request to the specified path with optional headers and parametersdelete(path:headers:parameters:completion:)
– Makes a DELETE request to the specified path with optional headers and parameters
networkingProvider.get(path: "/users/1", headers: nil, parameters: nil) { result in
switch result {
case .success(let response):
// Handle successful response
print(response.data)
case .failure(let error):
// Handle error
print(error.localizedDescription)
}
}
Response Handling
XMNetworking provides efficient handling of network responses. The response object contains the following properties:
data
– The response datastatusCode
– The HTTP status coderesponseHeaders
– The response headers
Error Handling
XMNetworking provides extensive error handling capabilities. In case of any network error or failure, the completion block will receive an error object with the relevant information.
Caching
XMNetworking supports response caching to optimize network requests. You can configure the cache using the URLCache
class and its related properties.
Tracking and Canceling Requests
XMNetworking allows you to track and cancel ongoing network requests using the unique request identifiers generated for each request.
- To track a request, use the returned request identifier.
- To cancel a request, call the cancelRequest(withIdentifier:) method of the XMNetworkingProvider instance and provide the request identifier.
// Making a GET request and tracking it
let requestId = networkingProvider.get(path: "/users/1", headers: nil, parameters: nil) { result in
// Handle response or error
}
// Later, if needed, cancel the request
networkingProvider.cancelRequest(withIdentifier: requestId)
Contributing
Contributions to XMNetworking are welcome! Feel free to submit issues and pull requests to help improve the library.
License
XMNetworking is available under the MIT license. See the LICENSE file for more info.