Web Services with Objective-C

22

description

Intro to writing web services clients in Objective-C

Transcript of Web Services with Objective-C

Page 2: Web Services with Objective-C

Hot New TechnologyWorld Wide Web – WWW

Web 3.0

Rise of the Machines!!!

[email protected] 2

Page 3: Web Services with Objective-C

Web services Web services – Websites for

programs

[email protected] 3

Page 4: Web Services with Objective-C

Common Web ServicesFlickr

Amazon

Twitter

Youtube

Ebay

Google search, maps, etc.

Meetup

Thousands of others

[email protected] 4

Page 5: Web Services with Objective-C

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.)

[email protected] 5

Page 6: Web Services with Objective-C

HTTPMethods: GET, POST, PUT, DELETE, HEAD

Parameters in URL or Body

Return codes: 200, 404, 500, etc.

Headers: Cache, Authentication, etc.

[email protected] 6

Page 7: Web Services with Objective-C

URI / URLAddressability

Connectedness

Uniformity

Hierarchical

[email protected] 7

Page 8: Web Services with Objective-C

ContentFormat up to the designer

XML

JSON – especially for AJAX

XHTML

Can support multiple formats

[email protected] 8

Page 9: Web Services with Objective-C

Steps for using a RESTful service

1 Gather information - URI, Method, Params

2 Make HTTP Request

3 Parse Result

[email protected] 9

Page 10: Web Services with Objective-C

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

[email protected] 10

Page 11: Web Services with Objective-C

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>

[email protected] 11

Page 12: Web Services with Objective-C

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>

[email protected] 12

Page 13: Web Services with Objective-C

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”

[email protected] 13

Page 14: Web Services with Objective-C

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];

[email protected] 14

Page 15: Web Services with Objective-C

More controlNSURLResponse *response;

NSError *error;

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

// Manipulate the request

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

[email protected] 15

Page 16: Web Services with Objective-C

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.

}

[email protected] 16

Page 17: Web Services with Objective-C

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

[email protected] 17

Page 18: Web Services with Objective-C

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

Page 19: Web Services with Objective-C

Other OptionsLibxml2

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

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

P Lists – NSPropertyListSerialization propertyListFromData

[email protected] 19

Page 20: Web Services with Objective-C

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?

[email protected] 20

Page 21: Web Services with Objective-C

Resourcesprogrammableweb.com

Code besides twitter engine?

For REST info check out:

[email protected] 21

Page 22: Web Services with Objective-C

Questions?Slides will be on http://www.E-String.com

Feel free to contact me:Julio Barros

[email protected]

[email protected] 22