To run the example project, clone the repo, and run pod install from the Example directory first.
// AppDelegate.swift
import SwiftyFetch
Fetch.shared.setBaseUrl("https://jsonplaceholder.typicode.com/")
Fetch.shared.setAPIKey("xxxxxxxxxxxxxxxxxxxx")
UserDefaults.standard.set("accessToken", forKey: "xxxxxxxxxxxxxxxxxxxx") // <- for testing; should be set by your auth controller
if let accessToken = UserDefaults.standard.value(forKey: "accessToken") as? String {
Fetch.shared.setToken(accessToken)
}// ViewController.swift
import SwiftyFetch
Fetch.shared.request("posts", method: "POST", body: ["limit": 25]) { result in
switch result {
case .success(let response):
if response.ok {
let json = response.json
print("JSON: \(json)")
} else {
print("HTTP: \(response.status), \(response.statusText)")
}
case .failure(let error):
print(error)
}
}response.status // HTTP status code
response.statusText // HTTP status description
response.ok // true if 200
response.headers // HTTP headers
response.url // full requested URL
response.text // string representation of response data
response.json // JSON response dataSwiftyFetch is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftyFetch', :git => 'https://github.com/charlesfries/SwiftyFetch.git'charlesfries, [email protected]
SwiftyFetch is available under the MIT license. See the LICENSE file for more info.