Consuming SOAP web services in iOS

Featured Image

In past few years I’ve been working on variety of different applications but never on one that includes consuming web services such as SOAP. That’s mostly because SOAP is considered outdated comparing to REST today – totally wrong assumption (comparing apples and oranges).

REST (Representational state transfer) – first of all requests and responses are built around the transfer of representations of resources. A resource can be essentially any coherent and meaningful concept that may be addressed. A representation of a resource is typically a document that captures the current or intended state of a resource. Main focus – data transfer.

SOAP (Simple Object Access Protocol) brings it’s own protocol and focuses on exposing pieces of application logic (not data) as services. It exposes operations and focuses on accessing named operations, each implement some business logic through different interfaces. That’s why SOAP is considered as successor of XML-RPC and also uses XML to encode its HTTP-based calls. Everything is done via HTTP POST request.

SOAP is mostly used for Enterprise applications to integrate wide types and no. of applications and another trend is to integrate with legacy systems. With that said, consuming SOAP should be an easy task. This is when WSDL comes in story.

WSDL (Web Services Description Language) is an XML-based language that is used for describing the functionality offered by a Web service. A WSDL description of a web service (also referred to as a WSDL file) provides a machine-readable description of how the service can be called, what parameters it expects, and what data structures it returns. It thus serves a roughly similar purpose as a method signature in a programming language.

To conclude introduction part, it’s good to know both of them. Enough with apples and oranges. Let’s see how to consume SOAP in iOS environment.

Having web service interface definition gives us opportunity to generate client side implementation automatically. Sudzc (http://sudzc.com/) is very good example of code generator for accessing SOAP-based web services on iOS platform. It basically generates all the backend part you need for your project. Although there are few things I would change in generated implementation my opinion is that Sudzc is good choice.

You can test Sudzc simply by going to http://sudzc.com/, uploading this WSDL and clicking Generate. Unzip downloaded and you should get fresh generated Xcode project. Open Xcode project and expand Source -> Examples.

As you can see Sudzc has generated all you need to consume SOAP web service, in this case Magento Core API. Find INMagentoService class and there you will see all API methods you can call. In each method you can pass delegate and feedback selector. Below you can see how to retrieve sales orders list.

- (void)salesOrderListCall
{
    INMagentoService* service = [INMagentoService service];
 
    // Returns NSMutableArray*. Retrieve list of orders by filters
    [service salesOrderList:self
                     action:@selector(salesOrderListHandler:)
                  sessionId:@""
                    filters:[[INfilters alloc] init]];
}
 
// Handle the response from salesOrderList.
- (void)salesOrderListHandler: (id) value
{
    // Handle errors
    if([value isKindOfClass:[NSError class]]) {
        NSLog(@"%@", value);
        return;
    }
 
    // Handle faults
    if([value isKindOfClass:[SoapFault class]]) {
        NSLog(@"%@", value);
        return;
    }
 
    // Do something with the NSMutableArray* result
    NSMutableArray* result = (NSMutableArray*)value;
    NSLog(@"salesOrderList returned the value: %@", result);
}

All API call examples are located in INMagentoServiceExample class. Just copy anything you need from there and paste it to your view controller. Now you are ready to consume SOAP web service in iOS environment.


8 comments

  1. Yes william, pico seems nice but unfortunately the wsdm compiler is nunable to create classes for v1 and v2 Magento soap
    Error compiling in both cases

  2. Oh, man!

    I’ve been months looking for something like that! At the end I wrote my own xml parsers, my classes, my methods, my feedbacks… And now all its done, after some MONTHS of pain (and no less than 20 classes, is a complex web service), I see this.

    I want to cry.

  3. I would recommend another tool called Pico, Pico is a web service client framework targeting iOS platform, its provides code generator tool based on Java Wsimport, also it provides a runtime which can automatically bind soap message with object model, Pico has been verified with industrial level wsdl like amazon and ebay wsdl. you may have a try.

  4. Hello there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my iphone4. I’m trying to find a template or plugin that might be able to resolve this issue.
    If you have any suggestions, please share. Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.