Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect iPhone and web service and get XML data?

You might find this tutorial, called Intro to SOAP Web Services useful. He shows how to package a request, send it to a web service, and read the response.

If you need some help with XML parsing, there is the TouchXML library which will give you a nice xml "document" to work with. Just be cautious of memory usage.

If you have to parse large XML message this tutorial about libxml and xmlreader in Cocoa will show you how to parse XML with the lower-level event-style parsers.


I've created an open source application for iPhone OS 3.0 that shows how to use REST & SOAP services in iPhone application, using XML (using 8 different iPhone libraries), SOAP, JSON (using SBJSON and TouchJSON), YAML, Protocol Buffers (Google serialization format) and even CSV from a PHP sample app (included in the project).

http://github.com/akosma/iPhoneWebServicesClient

The project is modular enough to support many other formats and libraries in the future.

The following presentation in SlideShare shows my findings in terms of performance, ease of implementation and payload characteristics:

http://www.slideshare.net/akosma/web-services-3439269


You can use these 2 lines which return the response of your HTTP request. You don't need any configuration. This code is usefull if you try to access a PHP scritp for example. After you just have to parse your result.

NSURL *URL=[[NSURL alloc] initWithString:stringForURL];
NSString *results = [[NSString alloc] initWithContentsOfURL :URL];

In my opinion, you have two options :

  • Use a third party library. You can try wsdl2objc. It didn't work for me, but it is under active development so it improves every day.
  • Use a raw HTTP connection and handle every request/response. This is the way I followed. It is hard, so I'd also like to know a better approach.