|
After giving up in my initial attempt to consume a WSDL service from the Mac, I decided to try out for myself what it would be like to consume a simple POX service (which is really nothing more than reading an XML document from disk or from an NSURL location). Turns out its really, really easy.
The code is nearly line-for-line copied from Hillegass' book. What this means is that the sample uses the table view column identifier to store the XPath required to pull that column from an XML Node. This highlights exactly everything that I hate about books and code samples found online - sure, simple 'hello world' apps illustrate the syntax but they don't illustrate good patterns and practices. If you are learning from Hillegass' book, please take the time to convert his sample from using XPath column identifiers to using actual model objects that are loosely coupled from the underlying data source. Please, for the love of all that is decent about Cocoa.
Friends don't let friends code tightly coupled.
Here's the code that gets executed in response to pushing a fetch button in the demo app:
- (IBAction)fetchFoodStars:(id)sender
{
[progress startAnimation:nil];
NSString *urlString = @"http://testbox:9000/SampleData.mvc";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if (!urlData)
{
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
return;
}
[doc release];
doc = [[NSXMLDocument alloc] initWithData:urlData options:0 error:&error];
if (!doc) {
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
return;
}
NSLog(@"Doc = %@", doc);
[itemNodes release];
itemNodes = [[doc nodesForXPath:@"//FoodNetworkStar" error:&error] retain];
[tableView reloadData];
[progress stopAnimation:nil];
}
Basically what this code is doing is opening a URL that's pointing at the location of my ASP.NET MVC application which has a controller that knows it should be dealing in POX and not HTML (turns out that is scary easy, as will be detailed in the next post).
Then this code converts the XML document retrieved into an array of XML nodes. The app controller acts as the data source for the table view, using the identifier value of each table view column as a key to retrieving the XPath that will retrieve the value for that column. For example, the 'name' column has an identifier of '@name', which tells the controller to get the value for the name column from the @name XPath (the name attribute on the current XML node).
Below you can see the results after pushing the fetch button. The table view took care of the binding and the call to reloadData on the tableview updated the values.

This is dead simple stuff. As I mentioned before, you do NOT want to be putting XPath in the column identifiers for your table views - that's tightly coupled HACKing. What you should be doing is converting the XML from your service into instances of your local data model (which is datasource-agnostic, isn't it???) and then your table view's data source will access the local data model objects instead of raw XML nodes. Once you get into trying to allow the user to interactively edit data that came from a web service, you'll thank me for recommending the local data model conversion.
In the next post, I'll show you the source code to the ASP.NET MVC application and the controller that I used as a POX service host. You'll be surprised that it was quicker and easier to implement the POX controller in ASP.NET MVC than it would've been in Ruby on Rails!
I tried to use WSDL Service some days ago and it went really fine until I
noticed that it is not available on the iPhone (but you can use it on the
Simulator). So what was your problem with WSDL Service?
POX.... wasn't familiar with that term so I googled it... first thing that
comes up were images of bodies covered in boils and sores... thanks a lot..
:P
Having to use MS tools is like having a pox...so, the name is appropriate.
:)
A while again, when people wanted to say "not SOAP" they used REST, which
didn't make much sense, so people started saying "Plain Old XML" ie. POX.
http://en.wikipedia.org/wiki/Plain_Old_XML