2. NSCoders México
Agenda
¿Que es alamofire?
¿Para que sirve?
¿Como se usa?
¿Cómo proceso la respuesta?
¿Cómo lo integro a un componente
(UITableview)?
DEMO
Acerca de mi
3. An HTTP networking library for iOS and OS X
this product develop for Alamofire Software Foundation.
4. ¿Para que sirve?
❖ Chainable Request / Response methods
❖ URL / JSON / plist Parameter Encoding
❖ Upload File / Data / Stream / MultipartFormData
❖ Download using Request or Resume data
❖ Authentication with NSURLCredential
❖ HTTP Response Validation
❖ TLS Certificate and Public Key Pinning
❖ Progress Closure & NSProgress
❖ cURL Debug Output
❖ Comprehensive Unit Test Coverage
5. ¿Como se usa?
❖ Agregando como POD
❖ source 'https://github.com/CocoaPods/Specs.git'
❖ platform :ios, '9.0'
❖ use_frameworks!
❖ pod 'Alamofire', '~> 3.3'
❖ Agregando de forma manual
❖ git submodule add https://github.com/Alamofire/Alamofire.git
❖ Abrir el proyecto compilando y generando el .Framework
6. ¿Como se usa?
//Importando la librería en la clase que se utilizara
import Alamofire
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
7. ¿Cómo proceso la respuesta?
Existen varias formas de procesar la respuesta que nos devuelve alamofire veamos un
ejemplo:
{
songs: [{
title: "1904",
artist: "The Tallest Man on Earth",
year: "2012",
web_url: "http://www.songnotes.cc/songs/78-the-tallest-man-on-earth-1904",
img_url: "http://fireflygrove.com/songnotes/images/artists/TheTallestManOnEarth.jpg"
}, {
title: "#40",
artist: "Dave Matthews",
year: "1999",
web_url: "http://www.songnotes.cc/songs/119-dave-matthews-40",
img_url: "http://fireflygrove.com/songnotes/images/artists/DaveMatthews.jpg"
}, {
title: "40oz to Freedom",
artist: "Sublime",
year: "1996",
web_url: "http://www.songnotes.cc/songs/45-sublime-40oz-to-freedom",
img_url: "http://fireflygrove.com/songnotes/images/artists/Sublime.png"
}, {
title: "#41",
artist: "Dave Matthews",
year: "1996",
web_url: "http://www.songnotes.cc/songs/46-dave-matthews-band-41",
img_url: "http://fireflygrove.com/songnotes/images/artists/DaveMatthews.jpg"
}]
}
8. ¿Cómo proceso la respuesta?
//Validamos que la respuesta contenga un NSArray con todos los
elementos de lista de canciones
if let JSON = response.result.value{
self.arraySongs = JSON[“songs"] as! NSArray
}
//Obtenemos un elemento del array y lo casteamos para imprimir su
valor
let dataCell = arraySongs[indexPath.row]
cell.textLabel?.text = dataCell["artist"] as? String
9. ¿Cómo lo integro en un UITableView?
❖ Declaramos una colección para alimentar el
UITableview
❖ poblamos la colección para llenar el tableView
❖ recargamos el tableview para poblarlo con el contenido
de la colección