简体   繁体   中英

sending and receiving data with a server for an iphone app

I would like to allow my users to see other users data. I am thinking in terms of a game. I would like the users game on their phone to connect to an online score list , and store the lowest score on the list. Then if the user scores above, their score gets put into the list and stored.

What does this involve exactly?

Will this need encryption? which will then need the iphone encryption requirements to be met.

I am not very knowledgeable on the server side, so would need to start at the basics. Will this even need a server, or could I setup a website and grab the data from there? this making the scores available outside of the app.

Thanks in advance

My suggestion is to setup a REST service that uses JSON-formatted data. This is far easier to deal with on the iPhone (and other platforms) than SOAP services.

Introductions:

REST: http://en.wikipedia.org/wiki/Representational_State_Transfer

JSON: http://www.json.org/

If you're familiar with .NET, Rick Strahl has a great blog on using WCF to setup a JSON/REST service: http://americanparanoia.com/Weblog/posts/164419.aspx and http://www.code-magazine.com/Article.aspx?quickid=080014 are nice places to start.

There are numerous other tutorials online for creating JSON/REST services in whatever language you'd like; .NET, Java, Ruby, PHP, etc.

You will need this JSON-parsing library for objective-c: http://code.google.com/p/json-framework/ That library will convert a string in JSON format into NSMutableDictionary and/or NSMutableArray (as well as the other way around).

A partial example of how you MIGHT get a list of top scores FROM the server:

NSError * error;
NSURLResponse * response;

NSMutableURLRequest * request = [NSMutableURLRequest
     requestWithURL:[NSURL URLWithString:@"https://myWebsite.com/topScores"]
     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
     timeoutInterval:60.0];

// Not sure if you need this, but I frequently do POSTs as well, so whatever:
[request setHTTPMethod:@"GET"];

NSData * responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString * json = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSMutableArray * scores = [json JSONRepresentation];

(NB: You'll have to check the error response, I just omitted it for clarity)

Also, ideally (since in my example I use the synchronous method) this should be run on a background thread... but I found it much easier to run this on the background thread manually using "performSelectorInBackground:" and use the synchronous methods, than I did using the async methods. Keep in mind, you'll have to create your own auto release pool if you do that... but that's two lines, and it's super easy.

A partial example of how you MIGHT send a new scores TO the server:

NSString * requestJson = [topScoresDictionary JSONRepresentation];
NSData * data = [requestJson dataUsingEncoding:NSUTF8StringEncoding];

NSError * error;
NSURLResponse * response;

NSMutableURLRequest * request = [NSMutableURLRequest
     requestWithURL:[NSURL URLWithString:@"https://myWebsite.com/topScores"]
     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
     timeoutInterval:60.0];

// POST the data to the server (just like a form submission):
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:[NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"content-type", nil]];
[request setHTTPBody:data];

NSData * responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString * json = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSMutableArray * scores = [json JSONRepresentation];

To answer your question about encryption... YES... you will want to do something to prevent people from tampering with your high-score service. Does this mean it's encryption? Well, maybe. Maybe it's just some sort of additional checksum. But ASSUME that even going over HTTPS people WILL be able to sniff your network traffic and reverse engineer it. You will want to thwart people like me who do that just to see if you're dull enough to leave your high score service wide open, because gee wouldn't it be neat to see my name as #1 with some ridiculous score... or worse.

Also, remember if you get way more hits that you expect, your service needs to scale (consider cloud hosting seriously... Azure, EC2, AppEngine, etc). These can be life savers. If your service gets slow and people are sitting around waiting for it, your application will suffer.

Also, check out this other post: SOAP, REST or just XML for Objective-C/iPhone vs. server solution

Obviously, I completely agree with the other poster about Game Center... if Game Center meets your needs, use that.

Your needs sound like a perfect match for Apple's Game center. There's no need to muck around with setting up your own REST API and server if the only features you want are already built into the OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM