• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
7,861
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
76
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Talking to web service APIs with Cocoa Or: How to build a Twitter client in 5 minutes
  • 2. Hot! http://www.programmableweb.com/apis/directory/
  • 3. Web service APIs • SOAP RPC over HTTP • requests and responses in XML • REST over HTTP • requests consist of a URI and a HTTP verb (GET, POST, PUT or DELETE) • responses typically in XML or JSON
  • 4. - (void)makeSynchronousRequest { NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; NSURLResponse *urlResponse; NSError *error; NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error]; NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; // do something with responseString }
  • 5. ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/
  • 6. - (void)makeSynchronousRequest { NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request start]; NSError *error = [request error]; if(!error) { // do something with [request responseString] } }
  • 7. - (void)makeAsynchronousRequest { NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { // do something with [request responseString] } - (void)requestFailed:(ASIHTTPRequest *)request { // do something with [request error] }
  • 8. Demo!
  • 9. JSON
  • 10. { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ], "newSubscription": false, "companyName": null }
  • 11. JSON.framework http://code.google.com/p/json-framework/
  • 12. JSON Objective-C object NSCFDictionary array NSCFArray string NSCFString number NSDecimalNumber true/false NSCFBoolean null NSNull
  • 13. [@"..." JSONValue];
  • 14. Demo!
  • 15. ?
  • 16. Jesper Särnesjö http://jesper.sarnesjo.org/ CocoaHeads Stockholm http://cocoaheads.org/se/Stockholm/