Reputation: 2851
I am building an IPhone application (first one) that needs to send data to the server and responds back data from the server. As im a .net developer I though of creating a web service with 2 call (GetData and SendData) with 1 parameter that sends in XML Data. These will send and get call which will include media content e.g. a picture.
What other options do I have to send data (media) to and from a server and what are the pros and cons?
Upvotes: 1
Views: 13026
Reputation: 36752
One option is to use Hessian. Hessian is a language agnostic protocol for web-services, and binary serialization for sending messages calls and responses. Some pros for Hessian:
You can implement the server in .NET using HessianC#. Hessian is based around interfaces so you could define this interface in C#:
public interface HelloService {
string GetGreeting();
}
That you would then implement with nothing more than:
public class HelloHandler : CHessianHandler, HelloService {
public string GetGreeting() {
return "Hello World";
}
}
On the iPhone side you can use HessianKit to setup the client. This is done in two steps, first the C# interace must be converted into a Objective-C protocol:
@protocol HelloService
-(NSString*)GetGreeting;
@end
Then you need to get a proxy to your web service fro this interface/protocol:
id<HelloService> proxy = [CWHessianConnection proxyWithURL:url
protocol:@protocol(HelloService)];
NSLog([proxy GetGreeting]);
Upvotes: 4
Reputation: 75058
.Net has great SOAP support. But SOAP is a terrible thing to use on the iPhone, it's not well supported and even if it were it's a really heavy protocol for what are likely very lightweight calls.
Look into what kinds of REST libraries are available for .Net - they will be easier to consume and lower in bandwidth. There are also REST wrappers for the iPhone.
When developing any server for mobile communications you should avoid SOAP if at all possible.
Upvotes: 1
Reputation: 28864
If you want to minimize your coding, you can get a quick HTTP reply from your server with a method call like this:
NSURL* url = [NSURL URLWithString:@"http://mysite.com/my_page.html"];
NSStringEncoding encoding;
NSError* error = nil;
NSString* pageData = [NSString stringWithContentsOfURL:url
usedEncoding:&encoding error:&error];
// Now pageData is a string with the html from that URL, or error will indicate
// any network error that occurred.
NSData
has a similar method called dataWithContentsOfURL:options:error:
which can handle getting binary data from a server. Both of these approaches are synchronous, meaning your code becomes blocked while it is awaiting a response from the server - at least until the timeout hits, or an error is detected.
For asynchronous network communications, you can use other methods in NSURLConnection
, which also works with companion classes NSURLRequest
, NSURLResponse
and NSURL
. The quickest way to learn this is to glance through the NSURLConnection
docs. Here's some example code of how to write an asynchronous HTTP get using these classes.
I'm assuming you have mainly HTTP transfers in mind, for which the above classes can handle most interactions, including different HTTP request types (e.g. post vs get), different encoding types or binary data, allowing your app to handle each packet as it arrives, hooking into http-level redirects, setting a custom timeout, etc.
There are still more ways to communicate, such as using Bonjour, which assists with server-less setups (such as two iPhones sharing a wi-fi connection); or Game Kit, which can handle peer-to-peer bluetooth connections, and has support designed for in-game voice communications.
Upvotes: 6