Web Services with Objective-C
-
Upload
juio-barros -
Category
Technology
-
view
36.406 -
download
1
description
Transcript of Web Services with Objective-C
Common Web ServicesFlickr
Amazon
Youtube
Ebay
Google search, maps, etc.
Meetup
Thousands of others
2 basic styles of servicesService Oriented Architectures – (Distributed
Objects)
CORBA, SOAP, XML-RPC, COM
Resource Oriented Architectures Resources can be anything REST – use simple web based technologies
HTTPURIXML (or Json, xhtml, text, plists, etc.)
HTTPMethods: GET, POST, PUT, DELETE, HEAD
Parameters in URL or Body
Return codes: 200, 404, 500, etc.
Headers: Cache, Authentication, etc.
ContentFormat up to the designer
XML
JSON – especially for AJAX
XHTML
Can support multiple formats
Steps for using a RESTful service
1 Gather information - URI, Method, Params
2 Make HTTP Request
3 Parse Result
1) Gather informationStudy 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
Result (part 1)<results>
<head>
<count>200</count>
<total_count>1274</total_count>
<updated>Mon Aug 25 20:10:21 EDT 2008</updated>
<description>API method for accessing meetup events</description>
<lat>40.743348</lat>
<id/>
<method>Events</method>
<lon>-73.993525</lon><title>Meetup
Events</title><next>http://api.meetup.com/events/
?... </next><link>http://
api.meetup.com/events/</link>
<url>http://api.meetup.com/events/
? …</url></head>
Result (part 2)<items>
<item>
<lon>-73.98999786376953</lon>
<rsvpcount>3</rsvpcount>
<group_name>Better Laugh Laughter Yoga</group_name>
<lat>40.7400016784668</lat>
<feecurrency>USD</feecurrency>
<time>Mon Aug 25 19:30:00 EDT 2008</time>
<event_url>http://stress.meetup.com/12/calendar/8385325</event_url>
<attendee_count>0</attendee_count>
<id>8385325</id>
<venue_lon/>
<fee>0.0</fee>
<venue_name/>
<venue_lat/>
<description/>
<photo_url>
http://photos1.meetupstatic.com/photos/event/1/6/4/b/global_3743707.jpeg
</photo_url>
<updated>Mon Jul 21 20:35:17 EDT 2008</updated>
<feedesc/>
<questions/>
<name>Better Laugh Laughter Yoga Meetup</name>
</item>
2) Make HTTP RequestEncode parameters URL or body
Specify method
Manipulate headers
Example: Simple GET in your browser or use curlcurl "http://api.meetup.com/groups/?
zip=10003&key=123”
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];
More controlNSURLResponse *response;
NSError *error;
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
// Manipulate the request
NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
Asynchronous wayNSURLConnection *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.
}
HomeworkExtract 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
3) Parse Results NSXMLParser - SAX based parsing
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];
parser: didStartElement: namespaceURI: qualifiedName: attributes:
parser: didEndElement: namespaceURI: qualifiedName:
parser: foundCharacters:[email protected] 18
Other OptionsLibxml2
http://code.google.com/p/touchcode/wiki/TouchXML
http://code.google.com/p/touchcode/wiki/TouchJSON
P Lists – NSPropertyListSerialization propertyListFromData
Things to look out forBugs 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?
Resourcesprogrammableweb.com
Code besides twitter engine?
For REST info check out:
Questions?Slides will be on http://www.E-String.com
Feel free to contact me:Julio Barros