Consuming a .NET Web Service from iPhone
This was something which take me a whole (working) day to implement, and I’d like to share the new-found knowledge with everybody interested. We’ll use SOAP with a simple .asmx web service (for the sake of simplicity). I’ll only focus on setting up the connection, so tasks such as authentication and security are to be implemented by you.
First, we’ll need the service. Our simple little web service will return void, and take two parameters, a user name and an integer value representing the user’s score (yes it’s a high score table). The service will take care of putting these values into a database. The SQL table looks like this:
|ID||Primary key, Identity seed (1,1)|
The code of the service (assuming you’ve created a ConnectionString called myString in the web.config, and included System.Web.Configuration and System.Data.SqlClient in your class:
public void postScore(string name, string score)
string connStr = WebConfigurationManager.ConnectionStrings[“myString”].ConnectionString;
SqlConnection theConnection = new SqlConnection(connStr);
String commandText = “INSERT INTO iPhone (Name, Score) VALUES (@Name, @Score)”;
SqlCommand theCommand = new SqlCommand (commandText, theConnection);
That was all you on the server side. Now publish your service on the Internet, (I’ll assume you’ve published it on http://testService.com, with an asmx file called theService. Now open http://testService.com/theService.asmx in the browser, and select the postScore operation. All information you’ll need to set up your request for the service will be written there.
I wouldn’t like to repeat myself, so I won’t post the setup information here, it’ll be written on the iPhone client. So let’s head towards it and write it quickly.
Now fire up xCode, and let’s write the client which consumes the service. It will look like this:
NSString* soapMessage = [NSString stringWithFormat:
@”<?xml version=\”1.0\” encoding=\”utf-8\”?>
</soap:Envelope>”, scoreInt, nameString];
NSURL* theUrl = [NSURL URLWithString:@http://testService.com/theService.asmx”];
NSString* msgLength = [NSString stringWithFormat:@”%d”, [soapMessage length]];
NSMutableRequest* theRequest = [NSMutableRequest requestWithURL:theUrl];
[theRequest addValue:@”text/xml; charset=utf-8” forHTTPHeaderField:@”Content-Type”];
[theRequest addValue:@”http://tempuri.org/postScore” forHTTPHeaderField:@”SOAPAction”];
[theRequest addValue:msgLength forHTTPHeaderField:@”Content-Length”];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate:self];
As soon as initWithRequest is called, the request will be sent to the given server (in an asynchronous manner). To see what comes back as the result, implement the following method:
-(void)connection: (NSURLConnection*) connection didReceiveData: (NSData*)data
Examine the value stored in data (encode it to a string and log it) to see the result of your request.
Consuming a .NET web service from an iPhone is relatively easy, as you’ve seen. Feel free to experiment (I’d say DO experiment) to gain a deeper understanding of the players.