0% found this document useful (0 votes)
60 views

Restful Web Services and Objective C: Nyc Iphone Developers Julio Barros

This document discusses RESTful web services and their use in Objective-C. It defines REST and describes how RESTful services use HTTP methods and URIs to access resources over the web. It provides examples of common REST APIs and outlines the three steps to using one: gathering API information, making an HTTP request, and parsing the response. Code examples in Objective-C demonstrate how to make requests and parse XML responses.

Uploaded by

Aymen Ben Maad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Restful Web Services and Objective C: Nyc Iphone Developers Julio Barros

This document discusses RESTful web services and their use in Objective-C. It defines REST and describes how RESTful services use HTTP methods and URIs to access resources over the web. It provides examples of common REST APIs and outlines the three steps to using one: gathering API information, making an HTTP request, and parsing the response. Code examples in Objective-C demonstrate how to make requests and parse XML responses.

Uploaded by

Aymen Ben Maad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

RESTful Web Services and 

Objective‐C
NYC iPhone Developers
Julio Barros
Julio@E‐String.com

Julio@E‐String.com
1
Hot New Technology
 World Wide Web – WWW

 Web 3.0

 Rise of the Machines!!!

Julio@E‐String.com
2
Web services 
Web services – Websites for programs

Julio@E‐String.com
3
Common  Web Services
 Flickr
 Amazon
 Twitter
 Youtube
 Ebay
 Google search, maps, etc.
 Meetup
 Thousands of others
Julio@E‐String.com
4
2 basic styles of services
 Service Oriented Architectures – (Distributed Objects)

  CORBA, SOAP, XML‐RPC, COM

 Resource Oriented Architectures
 Resources can be anything
 REST – use simple web based technologies
 HTTP
 URI
 XML (or Json, xhtml, text, plists, etc.)

Julio@E‐String.com
5
HTTP
 Methods: GET, POST, PUT, DELETE, HEAD

 Parameters in URL or Body

 Return codes: 200, 404, 500, etc.

 Headers: Cache, Authentication, etc.

Julio@E‐String.com
6
URI / URL
 Addressability

 Connectedness

 Uniformity

 Hierarchical

Julio@E‐String.com
7
Content
Format up to the designer

 XML

 JSON – especially for AJAX

 XHTML

Can support multiple formats

Julio@E‐String.com
8
Steps for using a RESTful service
1 Gather information ‐ URI, Method, Params

2 Make HTTP Request

3 Parse Result

Julio@E‐String.com
9
1) Gather information
 Study the API
http://www.meetup.com/meetup_api/docs/

 Decide what you want to do: Ie. Get a list of meetups near 
me.
http://api.meetup.com/events.xml/?
lat=40.743348&lon=‐73.993525&radius=1&key=123

Julio@E‐String.com
10
Result (part 1)
<results> <method>Events</method>
<head>
<lon>‐73.993525</lon>
<title>Meetup Events</title>
<count>200</count> <next>
<total_count>1274</total_count> http://api.meetup.com/
events/?... 
<updated>Mon Aug 25 20:10:21  </next>
EDT 2008</updated> <link>http://api.meetup.com/
<description>API method for  events/</link>
accessing meetup events</ <url>
description> http://api.meetup.com/
<lat>40.743348</lat> events/? …
</url>
<id/> </head>
Julio@E‐String.com
11
Result (part 2)
<items> <fee>0.0</fee>
<item> <venue_name/>
<lon>‐73.98999786376953</lon> <venue_lat/>
<rsvpcount>3</rsvpcount> <description/>
<group_name>Better Laugh Laughter Yoga</ <photo_url>
group_name>
http://photos1.meetupstatic.com/photos/
<lat>40.7400016784668</lat> event/1/6/4/b/global_3743707.jpeg
<feecurrency>USD</feecurrency> </photo_url>
<time>Mon Aug 25 19:30:00 EDT 2008</time> <updated>Mon Jul 21 20:35:17 EDT 2008</
updated>
<event_url>http://stress.meetup.com/12/
calendar/8385325</event_url> <feedesc/>
<attendee_count>0</attendee_count> <questions/>
<id>8385325</id> <name>Better Laugh Laughter Yoga Meetup</
name>
<venue_lon/>
</item>

Julio@E‐String.com
12
2) Make HTTP Request
 Encode parameters URL or body

 Specify method

 Manipulate headers

Example: Simple GET in your browser or use curl
curl "http://api.meetup.com/groups/?zip=10003&key=123”

Julio@E‐String.com
13
Simplest way in Objective‐C
NSString *urlString = 
    @"http://api.meetup.com/…”;

NSURL *url = [NSURL URLWithString:urlString];

NSStringEncoding encoding;
NSError *error;

NSString *doc = [NSString stringWithContentsOfURL:url 
  usedEncoding:&encoding error:&error];

Julio@E‐String.com
14
More control
NSURLResponse *response;

NSError *error;

NSMutableURLRequest *urlRequest = 
  [NSMutableURLRequest requestWithURL:url];

// Manipulate the request

NSData *urlData = [NSURLConnection 
  sendSynchronousRequest:urlRequest 
  returningResponse:&response error:&error];

Julio@E‐String.com
15
Asynchronous way
NSURLConnection *connection = [NSURLConnection 
connectionWithRequest:urlRequest delegate:self]; 
// start ‘progress’ animation

‐(void)connection:(NSURLConnection *)connection 
didReceiveData:(NSData *)data {
    [myData appendData:data];
}
‐ (void)connectionDidFinishLoading:(NSURLConnection 
*)connection {
  // stop ‘progress’ animation and use data.
}
Julio@E‐String.com
16
Homework
 Extract this all out into a reusable web services client class 
that
 Sets parameters and body based on HTTP method
 Makes asynchronous calls and collects data
 Calls back arbitrary methods on delegate
 Load images asynchronously also

 Hints: SEL/performSelector, also check out twitter engine

Julio@E‐String.com
17
3) Parse Results
 NSXMLParser ‐  SAX based parsing

  NSXMLParser *parser = [[NSXMLParser alloc] 
initWithData:data];

// also initWithContentsOfURL

  [parser setDelegate:self];

  [parser parse];

Julio@E‐String.com
18
Parser Delegate

parser: didStartElement: namespaceURI: qualifiedName: 
attributes:

parser: didEndElement: namespaceURI: qualifiedName:

parser: foundCharacters:

Julio@E‐String.com
19
Other Options
 Libxml2

 http://code.google.com/p/touchcode/wiki/TouchXML

 http://code.google.com/p/touchcode/wiki/TouchJSON

 P Lists – NSPropertyListSerialization 
propertyListFromData

Julio@E‐String.com
20
Things to look out for
 Bugs in client libraries ‐ sometimes PUT and DELETE are 
not well supported

 Debugging the HTTP request response can be trying. 
Anyone know of a good debugging proxy?

Julio@E‐String.com
21
Resources
 programmableweb.com

 Code besides twitter engine?

 For REST info check out:

Julio@E‐String.com
22
Questions?
Slides will be on http://www.E‐String.com

Feel free to contact me:
Julio Barros
917‐445‐7264
Julio@E‐String.com

Julio@E‐String.com
23

You might also like