iPhone/iPad Apps
In today’s marketplace users are looking for applications that extend their functionality to their iPhone/iPad device. Building business applications on the iPhone/iPad that communicating with web services that provides that functionality in the cloud is a great way to embrace this build forward thinking revolution. However, consuming web services in an iPhone application is not for the faint-of-heart. Unlike other development tools (such as Microsoft Visual Studio), Xcode does not have built-in tools that make consuming web services easy. Everything must be done by hand and you need to know how to form the relevant XML (SOAP) messages to send to the web services and then parse the returning XML result.
Using SOAP 1.1
One way to consume this web service is to use SOAP (Simple Object Access Protocol). When using SOAP, you use the HTTP POST (or securely over HTTPS) method to send a request to the web service. In a browser open the url: http://host05slc.centershift.com/iService/iService1.asmx The following is a sample SOAP 1.1 request and response.
The placeholders shown need to be replaced with actual values.
POST /iService/iService1.asmx HTTP/1.1
Host: host05slc.centershift.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://centershift.com/GetSiteList"
string</UserName>
<Password>string
<!--GetSiteList>
To get a request to work from within xCode/Objective C
-(void)GetSiteList{
NSString *soapMessage = [NSString stringWithFormat:@
“xml version=\”1.0\” encoding=\”utf-8\”?>\n”
“<soap:Body>\n”
“<UserName>%@</UserName>\n”
“<Password>%@</Password>\n”
“</GetSiteList>\n”
“</soap:Body>\n”
“</soap:Envelope>\n”, [[NSUserDefaults standardUserDefaults] stringForKey:@”MyCSUserName”], [[NSUserDefaults standardUserDefaults] stringForKey:@”MyCSPassword”] ];
// Web service I setup to do this test application.
// Setup the web request with the url we created
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
// need length of the soap xml for the header in the html file
NSString *msgLength = [NSString stringWithFormat:@”%d”, [soapMessage length]];
// add our htm headers which is a must for a WebService
[theRequest addValue:@”text/xml; charset=utf-8″ forHTTPHeaderField:@”Content-Type”];
[theRequest addValue:msgLength forHTTPHeaderField:@”Content-Length”];
//We are doing a HTML POST
[theRequest setHTTPMethod:@”POST”];
// set the HTML Body, which is the XML Soap message we created above. Needs to be UTF8 encoding
// Needs to be of type Data [theRequest setHTTPBody: [soapMessage dataUsingEncoding: NSUTF8StringEncoding]];
// create the connection with the request
// and start loading the data, also set delegate to self
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
// Create the NSMutableData that will hold
webData=[[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
NSLog(@”ERROR”);
}
}
That’s the essence to format the SOAP message and post the request. In Objective C you’ll need to add the following for the methods of the NS objects we use:
– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// this method is called when the server has determined that it
// has enough information to create the NSURLResponse
// clear any data that may be around.
[webData setLength:0];
}
– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the webData
// webData is declared in the header file
[webData appendData:data];
}
– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Insert your code here to handle the problems created by connection failing with error
// Some typical things to handle:
// Release the connection, and the data object
[connection release];
[webData release];
// Display Alert UIAlertView
*errorNoConnection = [[UIAlertView alloc] initWithTitle:@”No Internet Connection” message:@”Unable to load site list” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[errorNoConnection show];
[errorNoConnection autorelease];
// Stop Indicator
[busySignal stopAnimating];
}
– (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//Manipulate data here and release Data and Connection
NSLog(@”Succeeded! Received %d bytes of data\n”,[webData length]);
NSString *theXml = [[NSString alloc]initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
[theXml release];
// Parse received xml from web service
// We’ll insert some logic here to parse the XML in Part 2 of this post
// release the connection, and the data object
[connection release];
[webData release];
}
This is a pretty vanilla service to call but all basic nuts and bolts are all there to call a web service in Objective C. In part 2 we’ll look at what we need to do to parse the xml and do something useful with the data. Please feel free to comment or email me with any questions or suggestions you’d like to make in relation to this post.