A lightweight Swift library for making web requests and consuming RESTful APIs!
In your Podfile add:
pod 'GTRest'
Don't forget to import GTRest anywhere you want to use it in your project:
import GTRest
Download or clone the repository, and add the files in GTRest/Source directory to your project.
Making web requests with GTRest is really simple and straightforward!
See the Documentation generated by jazzy on how to use GTRest.
A quick example:
let url = URL(...) // A URL object.
let rest = GTRest()
// Set any request HTTP headers:
rest.requestHttpHeaders.add(value: "application/json", forKey: "Accept")
// Set any URL query parameters:
rest.urlQueryParameters.add(value: "2", forKey: "page")
// Make a request.
rest.makeRequest(toURL: url, httpMethod: .get) { [unowned self] (results) in // or [weak self] (results) in
// Access data returned by the server:
if let data = results.data {
// Perform app-specific actions
}
// Access the response:
if let response = results.response {
// Do something with the response object if necessary.
// Checking the HTTP status code :
if (200...299).contains(response.httpStatusCode) {
// Successful request.
} else { ... }
}
// Access the error:
if let error = results.error {
// Do something with the error.
}
// Always update your UI on main thread:
DispatchQueue.main.async {
// Update UI.
}
}
iOS 11.0 and above.
You might be also interested in GTNetMon, another Swift library to get network status and connection information, and to monitor for network changes.