Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of JSON API call in Swift
//
// API.swift
//
// Created by Taro Minowa on 6/10/14.
// Copyright (c) 2014 Higepon Taro Minowa. All rights reserved.
//
import Foundation
typealias JSONDictionary = Dictionary<String, AnyObject>
typealias JSONArray = Array<AnyObject>
class API: NSObject, NSURLConnectionDataDelegate {
enum Path {
case SIGIN_IN
case GET_ROOMS
case GET_MESSAGES
case CREATE_MESSAGE
}
typealias APICallback = ((AnyObject?, NSError?) -> ())
let responseData = NSMutableData()
var statusCode:Int = -1
var callback: APICallback! = nil
var path: Path! = nil
func getRooms(user: User, callback: APICallback) {
let url = "\(Config.baseURL())/api/rooms.json?user_email=\(user.email)&user_token=\(user.token)"
makeHTTPGetRequest(Path.GET_ROOMS, callback: callback, url: url)
}
func getMessages(room: Room, user: User, callback: APICallback) {
let url = "\(Config.baseURL())/api/rooms/\(room.id)/messages.json?user_email=\(user.email)&user_token=\(user.token)"
makeHTTPGetRequest(Path.GET_MESSAGES, callback: callback, url: url)
}
func createMessage(room: Room, text: String, user: User, callback: APICallback) {
let url = "\(Config.baseURL())/api/rooms/\(room.id)/messages.json"
let body = "text=\(text)&user_email=\(user.email)&user_token=\(user.token)"
makeHTTPPostRequest(Path.CREATE_MESSAGE, callback: callback, url: url, body: body)
}
func signIn(email: String!, password: String!, callback: APICallback) {
let url = "\(Config.baseURL())/api/sessions.json"
let body = "user[email]=\(email)&user[password]=\(password)"
makeHTTPPostRequest(Path.SIGIN_IN, callback: callback, url: url, body: body)
}
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
let httpResponse = response as NSHTTPURLResponse
statusCode = httpResponse.statusCode
switch (httpResponse.statusCode) {
case 201, 200, 401:
self.responseData.length = 0
default:
println("ignore")
}
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
self.responseData.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
var error: NSError?
var json : AnyObject! = NSJSONSerialization.JSONObjectWithData(self.responseData, options: NSJSONReadingOptions.MutableLeaves, error: &error)
if error {
callback(nil, error)
return
}
switch(statusCode, self.path!) {
case (200, Path.SIGIN_IN):
callback(self.handleSignIn(json), nil)
case (200, Path.GET_ROOMS):
self.callback(self.handleGetRooms(json), nil)
case (200, Path.GET_MESSAGES):
callback(self.handleGetMessages(json), nil)
case (201, Path.CREATE_MESSAGE):
callback(self.handleCreateMessage(json), nil)
case (401, _):
callback(nil, handleAuthError(json))
default:
// Unknown Error
callback(nil, nil)
}
}
func handleAuthError(json: AnyObject) -> NSError {
if let resultObj = json as? JSONDictionary {
// beta2 workaround
if let messageObj: AnyObject = resultObj["error"] {
if let message = messageObj as? String {
return NSError(domain:"signIn", code:401, userInfo:["error": message])
}
}
}
return NSError(domain:"signIn", code:401, userInfo:["error": "unknown auth error"])
}
func handleSignIn(json: AnyObject) -> User? {
if let resultObj = json as? JSONDictionary {
if let userObj: AnyObject = resultObj["user"] {
if let userJson = userObj as? JSONDictionary {
if let user = User.createFromJson(userJson) {
return user
}
}
}
}
return nil
}
func handleCreateMessage(json: AnyObject) -> Message? {
if let messageObject = json as? JSONDictionary {
return Message.createFromJson(messageObject)
} else {
return nil
}
}
func handleGetRooms(json: AnyObject) -> Array<Room> {
var rooms = Array<Room>()
if let roomObjects = json as? JSONArray {
for roomObject: AnyObject in roomObjects {
if let roomJson = roomObject as? JSONDictionary {
if let room = Room.createFromJson(roomJson) {
rooms.append(room)
}
}
}
}
return rooms;
}
func handleGetMessages(json: AnyObject) -> Array<Message> {
var messages = Array<Message>()
if let messageObjects = json as? JSONArray {
for messageObject: AnyObject in messageObjects {
if let messageJson = messageObject as? JSONDictionary {
if let message = Message.createFromJson(messageJson) {
messages.append(message)
}
}
}
}
return messages;
}
// private
func makeHTTPGetRequest(path: Path, callback: APICallback, url: NSString) {
self.path = path
self.callback = callback
let request = NSURLRequest(URL: NSURL(string: url))
let conn = NSURLConnection(request: request, delegate:self)
if (conn == nil) {
callback(nil, nil)
}
}
func makeHTTPPostRequest(path: Path, callback: APICallback, url: NSString, body: NSString) {
self.path = path
self.callback = callback
let request = NSMutableURLRequest(URL: NSURL(string: url))
request.HTTPMethod = "POST"
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
let conn = NSURLConnection(request: request, delegate:self)
if (conn == nil) {
callback(nil, nil)
}
}
}
@flyingzl

This comment has been minimized.

Copy link

@flyingzl flyingzl commented Jul 11, 2014

Cool!

@matthewbga

This comment has been minimized.

Copy link

@matthewbga matthewbga commented Jul 24, 2014

Thanks for this. A terrific example!

@jskidd3

This comment has been minimized.

Copy link

@jskidd3 jskidd3 commented Oct 21, 2014

This is great, thanks! Saved me a lot of time.

@511430

This comment has been minimized.

Copy link

@511430 511430 commented Jan 6, 2015

Thanks! Helped me a lot

@benguthrie

This comment has been minimized.

Copy link

@benguthrie benguthrie commented Feb 18, 2015

Do you have an example of how to call one of these requests?

@mikesteele

This comment has been minimized.

Copy link

@mikesteele mikesteele commented Mar 17, 2015

@benguthrie:

var api = API()
api.getRooms(user, { (data, error) in 
    if (error == nil) {
        // do stuff with data
    }
})
@nainyshah

This comment has been minimized.

Copy link

@nainyshah nainyshah commented May 30, 2015

Need to know how to handle signin call can you plz give example

@paulomcnally

This comment has been minimized.

Copy link

@paulomcnally paulomcnally commented Dec 24, 2015

NSURLConnection deprecated on iOS 9.0

@agrakhov

This comment has been minimized.

Copy link

@agrakhov agrakhov commented Apr 28, 2016

Awesome example, saved me a ton of time!
Many thanks!

@vijaysss

This comment has been minimized.

Copy link

@vijaysss vijaysss commented Jan 31, 2017

please give a code singleton api response create call another class swift 3.0

@PrefabPanda

This comment has been minimized.

Copy link

@PrefabPanda PrefabPanda commented May 2, 2018

Perfect example, I was scratching my head about doing this simple thing for far too long. Works great on Swift 4.

@pdgoyal

This comment has been minimized.

Copy link

@pdgoyal pdgoyal commented Aug 15, 2018

Im encountering error in using NSURLConnection, it says NSURLConnection deprecated. I saw some solutions to use URLSession instead. How can I replace NSURLConnection to URLSession if I will insert it in your code? Thank you.

@pdgoyal

This comment has been minimized.

Copy link

@pdgoyal pdgoyal commented Aug 17, 2018

How can I use handleSignin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.