iOS networking library written in Swift
Setup: How to add a Git repository to your Xcode project
Usage:
Globally, eg, at the top of AppDelegate.swift, do:
let api = API(baseURL: URL(string: "https://api.example.com"), credentials: "MyApp:Secret")Let's define some local variables:
let email = "john.appleseed@example.com"
let fields = ["name": "John Appleseed", "email": email]To make a GET request:
let request = api.request("GET", "/users")To make a form POST request:
let request = api.request("POST", "/users", fields)If certain API paths require an access token, set it:
api.accessToken = "fb2e77d.47a0479900504cb3ab4a1f626d174d2d"To make an authenticated request, which includes your access token in the Authorization request header field:
let request = api.request("GET", "/me", authenticated: true)To send any of the requests above, use URLSession:
let dataTask = URLSession.shared.dataTask(with: request) { data, response, error in
// Handle response
}For help parsing a JSON response and converting an unsuccessful status code into an error, use API:
let dataTask = api.dataTask(with: request) { object, response, error in
// Handle response
}To make and send a multipart (file-upload) request:
let jpegData = UIImage(named: "JohnAppleseed.jpg")!.jpegData(compressionQuality: 0.75)
let request = api.request("POST", "/users", fields, jpegData)
let body = request.httpBody!
let dataTask = URLSession.shared.uploadTask(with: request, from: body) { data, response, error in
// Handle response
}Use HTTP instead of api for making one-off requests to resources with different base URLs.
See the Acani Chats iPhone Client for example usage.
See Networking.swift for other handy functions not mentioned here.
Released under the Unlicense.