Web Services with Objective-C

39,642 views

Published on

Intro to writing web services clients in Objective-C

Published in: Technology
3 Comments
8 Likes
Statistics
Notes
  • Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition) (Big Nerd Ranch Guides) --- http://amzn.to/1R982Rx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Objective C: Crash Course - The Ultimate Beginner's Course to Learning Objective C Programming in Under 12 Hours --- http://amzn.to/1T2JKuO
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Programming in Objective-C (6th Edition) (Developer's Library) --- http://amzn.to/21Bvido
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
39,642
On SlideShare
0
From Embeds
0
Number of Embeds
358
Actions
Shares
0
Downloads
486
Comments
3
Likes
8
Embeds 0
No embeds

No notes for slide

Web Services with Objective-C

  1. 1. [email_address]
  2. 2. Hot New Technology <ul><li>World Wide Web – WWW </li></ul><ul><li>Web 3.0 </li></ul><ul><li>Rise of the Machines!!! </li></ul>[email_address]
  3. 3. Web services <ul><li>Web services – Websites for programs </li></ul>[email_address]
  4. 4. Common Web Services <ul><li>Flickr </li></ul><ul><li>Amazon </li></ul><ul><li>Twitter </li></ul><ul><li>Youtube </li></ul><ul><li>Ebay </li></ul><ul><li>Google search, maps, etc. </li></ul><ul><li>Meetup </li></ul><ul><li>Thousands of others </li></ul>[email_address]
  5. 5. 2 basic styles of services <ul><li>Service Oriented Architectures – (Distributed Objects) </li></ul><ul><li>CORBA, SOAP, XML-RPC, COM </li></ul><ul><li>Resource Oriented Architectures </li></ul><ul><ul><li>Resources can be anything </li></ul></ul><ul><ul><li>REST – use simple web based technologies </li></ul></ul><ul><ul><ul><li>HTTP </li></ul></ul></ul><ul><ul><ul><li>URI </li></ul></ul></ul><ul><ul><ul><li>XML (or Json, xhtml, text, plists, etc.) </li></ul></ul></ul>[email_address]
  6. 6. HTTP <ul><li>Methods: GET, POST, PUT, DELETE, HEAD </li></ul><ul><li>Parameters in URL or Body </li></ul><ul><li>Return codes: 200, 404, 500, etc. </li></ul><ul><li>Headers: Cache, Authentication, etc. </li></ul>[email_address]
  7. 7. URI / URL <ul><li>Addressability </li></ul><ul><li>Connectedness </li></ul><ul><li>Uniformity </li></ul><ul><li>Hierarchical </li></ul>[email_address]
  8. 8. Content <ul><li>Format up to the designer </li></ul><ul><li>XML </li></ul><ul><li>JSON – especially for AJAX </li></ul><ul><li>XHTML </li></ul><ul><li>Can support multiple formats </li></ul>[email_address]
  9. 9. Steps for using a RESTful service <ul><li>Gather information - URI, Method, Params </li></ul><ul><li>Make HTTP Request </li></ul><ul><li>Parse Result </li></ul>[email_address]
  10. 10. 1) Gather information <ul><li>Study the API </li></ul><ul><ul><li>http://www.meetup.com/meetup_api/docs/ </li></ul></ul><ul><li>Decide what you want to do: Ie. Get a list of meetups near me. </li></ul><ul><ul><li>http://api.meetup.com/events.xml/?lat=40.743348&lon=-73.993525&radius=1&key=123 </li></ul></ul>[email_address]
  11. 11. Result (part 1) <ul><li><results> </li></ul><ul><li><head> </li></ul><ul><li><count>200</count> </li></ul><ul><li><total_count>1274</total_count> </li></ul><ul><li><updated>Mon Aug 25 20:10:21 EDT 2008</updated> </li></ul><ul><li><description>API method for accessing meetup events</description> </li></ul><ul><li><lat>40.743348</lat> </li></ul><ul><li><id/> </li></ul><ul><li><method>Events</method> </li></ul><ul><li><lon>-73.993525</lon> </li></ul><ul><li><title>Meetup Events</title> </li></ul><ul><li><next> </li></ul><ul><li>http://api.meetup.com/events/ ?... </li></ul><ul><li></next> </li></ul><ul><li><link>http://api.meetup.com/events/</link> </li></ul><ul><li><url> </li></ul><ul><li>http://api.meetup.com/events/ ? … </li></ul><ul><li></url> </li></ul><ul><li></head> </li></ul>[email_address]
  12. 12. Result (part 2) <ul><li><items> </li></ul><ul><li><item> </li></ul><ul><li><lon>-73.98999786376953</lon> </li></ul><ul><li><rsvpcount>3</rsvpcount> </li></ul><ul><li><group_name>Better Laugh Laughter Yoga</group_name> </li></ul><ul><li><lat>40.7400016784668</lat> </li></ul><ul><li><feecurrency>USD</feecurrency> </li></ul><ul><li><time>Mon Aug 25 19:30:00 EDT 2008</time> </li></ul><ul><li><event_url>http://stress.meetup.com/12/calendar/8385325</event_url> </li></ul><ul><li><attendee_count>0</attendee_count> </li></ul><ul><li><id>8385325</id> </li></ul><ul><li><venue_lon/> </li></ul><ul><li><fee>0.0</fee> </li></ul><ul><li><venue_name/> </li></ul><ul><li><venue_lat/> </li></ul><ul><li><description/> </li></ul><ul><li><photo_url> </li></ul><ul><li>http://photos1.meetupstatic.com/photos/event/1/6/4/b/global_3743707.jpeg </li></ul><ul><li></photo_url> </li></ul><ul><li><updated>Mon Jul 21 20:35:17 EDT 2008</updated> </li></ul><ul><li><feedesc/> </li></ul><ul><li><questions/> </li></ul><ul><li><name>Better Laugh Laughter Yoga Meetup</name> </li></ul><ul><li></item> </li></ul>[email_address]
  13. 13. 2) Make HTTP Request <ul><li>Encode parameters URL or body </li></ul><ul><li>Specify method </li></ul><ul><li>Manipulate headers </li></ul><ul><li>Example: Simple GET in your browser or use curl </li></ul><ul><ul><li>curl &quot;http://api.meetup.com/groups/?zip=10003&key=123” </li></ul></ul>[email_address]
  14. 14. Simplest way in Objective-C <ul><li>NSString *urlString = </li></ul><ul><li>@&quot;http://api.meetup.com/…”; </li></ul><ul><li>NSURL *url = [NSURL URLWithString:urlString]; </li></ul><ul><li>NSStringEncoding encoding; </li></ul><ul><li>NSError *error; </li></ul><ul><li>NSString *doc = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]; </li></ul>[email_address]
  15. 15. More control <ul><li>NSURLResponse *response; </li></ul><ul><li>NSError *error; </li></ul><ul><li>NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; </li></ul><ul><li>// Manipulate the request </li></ul><ul><li>NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; </li></ul>[email_address]
  16. 16. Asynchronous way <ul><li>NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self]; </li></ul><ul><li>// start ‘progress’ animation </li></ul><ul><li>-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { </li></ul><ul><li>[myData appendData:data]; </li></ul><ul><li>} </li></ul><ul><li>- (void)connectionDidFinishLoading:(NSURLConnection *)connection { </li></ul><ul><li>// stop ‘progress’ animation and use data. </li></ul><ul><li>} </li></ul>[email_address]
  17. 17. Homework <ul><li>Extract this all out into a reusable web services client class that </li></ul><ul><ul><li>Sets parameters and body based on HTTP method </li></ul></ul><ul><ul><li>Makes asynchronous calls and collects data </li></ul></ul><ul><ul><li>Calls back arbitrary methods on delegate </li></ul></ul><ul><ul><li>Load images asynchronously also </li></ul></ul><ul><li>Hints: SEL/performSelector, also check out twitter engine </li></ul>[email_address]
  18. 18. 3) Parse Results <ul><li>NSXMLParser - SAX based parsing </li></ul><ul><li>NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; </li></ul><ul><li>[parser setDelegate:self]; </li></ul><ul><li>[parser parse]; </li></ul><ul><li>parser: didStartElement: namespaceURI: qualifiedName: attributes: </li></ul><ul><li>parser: didEndElement: namespaceURI: qualifiedName: </li></ul><ul><li>parser: foundCharacters: </li></ul>[email_address]
  19. 19. Other Options <ul><li>Libxml2 </li></ul><ul><li>http://code.google.com/p/touchcode/wiki/TouchXML </li></ul><ul><li>http://code.google.com/p/touchcode/wiki/TouchJSON </li></ul><ul><li>P Lists – NSPropertyListSerialization propertyListFromData </li></ul>[email_address]
  20. 20. Things to look out for <ul><li>Bugs in client libraries - sometimes PUT and DELETE are not well supported </li></ul><ul><li>Debugging the HTTP request response can be trying. Anyone know of a good debugging proxy? </li></ul>[email_address]
  21. 21. Resources <ul><li>programmableweb.com </li></ul><ul><li>Code besides twitter engine? </li></ul><ul><li>For REST info check out: </li></ul>[email_address]
  22. 22. Questions? <ul><li>Slides will be on http://www.E-String.com </li></ul><ul><li>Feel free to contact me: </li></ul><ul><li>Julio Barros </li></ul><ul><li>917-445-7264 </li></ul><ul><li>[email_address] </li></ul>[email_address]

×