×
Copy
Open
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Tiny Networking @chriseidhof
Slide 2
Slide 2 text
$ pod install AlamoFire
Slide 3
Slide 3 text
Live coding!
Slide 4
Slide 4 text
$ rake routes | grep sign_in ... api_user_session POST /api/users/sign_in(.:format)
Slide 5
Slide 5 text
$ curl "http://localhost:3000/api/users/sign_in" \ -d "{\"email\": \"
[email protected]
\", \"password\": \"testtes\"}" \ --header "Content-Type:application/json" \ {"message":"Error with your login or password"}~
Slide 6
Slide 6 text
$ curl "http://localhost:3000/api/users/sign_in" -d "{\"email\": \"
[email protected]
\", \"password\": \"testtest\"}" --header "Content-Type:application/json" {"auth_token":"8yBZFpqhagTJr6DpDSyS","email":"
[email protected]
"}
Slide 7
Slide 7 text
func apiRequest
( baseURL: NSURL, resource: Resource
, failure: () -> (), completion: A -> () )
Slide 8
Slide 8 text
struct Resource
{ let path: String let method : Method let headers : [String:String] let requestBody: NSData? let parse: NSData -> A? }
Slide 9
Slide 9 text
typealias JSONDictionary = [String:AnyObject] func decodeJSON(data: NSData) -> JSONDictionary? func encodeJSON(dictionary: JSONDictionary) -> NSData?
Slide 10
Slide 10 text
Usage
Slide 11
Slide 11 text
let parse : NSData -> String? = { data in if let jsonDict = decodeJSON(data) { return jsonDict["auth_token"] as? String } return nil }
Slide 12
Slide 12 text
let requestParameters = ["email": "
[email protected]
", "password": "testtest"] let jsonBody = encodeJSON(requestParameters)
Slide 13
Slide 13 text
let path = "api/users/sign_in" let headers = ["Content-Type": "application/json", "Accept": "application/json"] let resource = Resource(path: path, method: .POST, requestBody: jsonBody, headers: headers, parse: parse)
Slide 14
Slide 14 text
let baseURL = NSURL(string:"http://localhost:3000")! apiRequest(baseURL, resource, { println("failure") }, { authorizationToken in println("Auth token \(authorizationToken)") })
Slide 15
Slide 15 text
let email = "
[email protected]
" let password = "testtest" let baseURL = NSURL(string:"http://localhost:3000")! let resource = login(email, password) apiRequest(baseURL, resource, { println("failure") }, { authorizationToken in println("Auth token \(authorizationToken)") })
Slide 16
Slide 16 text
More live coding!
Slide 17
Slide 17 text
Resources — https:/ /gist.github.com/chriseidhof/ 26bda788f13b3e8a279c — http:/ /chris.eidhof.nl/posts/tiny-networking-in- swift.html — http:/ /www.objc.io/books/
Slide 18
Slide 18 text
Thanks?
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
No content