Creating a highscore like system, iPhone side

2019-06-14 02:09发布

I'm sorry for opening a new question, I had to - as I wrote the other question from my iPhone as unregistered user and it is not very comfortable to write from the iPhone.

Rephrasing the question:

Is it possible to use the:

[NSMutableArray writeToURL:(NSString *)path atomically:(BOOL)AuxSomething];

In order to send a file (NSMutableArray) XML file to a url, and update the url to contain that file?

for example: I have an array and I want to upload it to a specific URL and the next time the app launches I want to download that array.

NSMutableArray *arrayToWrite = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",nil];

[arrayToWrite writeToURL:

[NSURL urlWithString:@"mywebsite.atwebpages.com/myArray.plist"] atomically:YES];

And at runtime:

NSMutableArray *arrayToRead =

[[NSMutableArray alloc] initWithContentsOfURL:[NSURL urlWithString:@"mywebsite.atwebpages.com/myArray.plist"]];

Meaning, I want to write an NSMutableArray to a URL, which is on a web hosting service
(e.g. batcave.net), the URL receives the information and updates server sided files accordingly. A highscore like setup, user sends his scores, the server updates it's files, other users download the highscores at runtime.

I hope this is clarified.

Edit: What I am looking for is scripting PHP or ASP so the website, the URL where the data is sent to would know how to handle it. I want an example or a tutorial on how to implement this scripting for handling data, if it's possible to do this on a web hosting service.

~Thanks in advance.

2条回答
倾城 Initia
2楼-- · 2019-06-14 02:25

According to NSData writeToURL docs (at least for iPhone OS 2.2.1):

"Since at present only file:// URLs are supported, there is no difference between this method and writeToFile:atomically:, except for the type of the first argument."

Although the docs for NSArray/NSDictionary/NSString do not specifically mention the restriction, it would seem highly likely that the same restriction applies.

So you will have to upload the XML using some other mechanism.

Also, web sites generally are read only, unless you provide specific code on the web server to support uploading.

查看更多
相关推荐>>
3楼-- · 2019-06-14 02:28

To answer the question "How do I create a high score like system?", there are multiple parts of the system:

  • You need an ID for each user (a GUID generated on the iPhone, together with the users name should be sufficient).
  • You need a server that: remembers high scores; receives high scores from users; either displays (on a web site) the high scores and/or makes the high scores available for download to the phone.
  • You need some fraud protection, although that is likely fighting a losing battle against jailbreakers.

On the iPhone app side, you might want to be able to download the current high scores for display, which is done easily enough with something like:

int statusCode = 0;
NSData* result = nil;
NSHTTPURLResponse* response = nil;
NSError* error = nil;
NSString* url = @"http://www.yourserver.com/highscores.php"; // returns XML plist data
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:180];
result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//  NSLog( @"NSURLConnection result %d %@ %@", [response statusCode], [request description], [error description] );
statusCode = [response statusCode];
if ( (statusCode == 0) || (!result && statusCode == 200) ) {
    statusCode = 500;
}

Since it is synchronous, you might want to put it inside an NSOperation. Alternatively, you can use

+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate

To send high score data, because it is so small, the easiest way is simply to encode it in the URL.

NSString* url = [NSString stringWithFormat:@"http://www.yourserver.com/sethighscores.php?uid=%@;name=%@;score=%d;antifraud=%@", [uid encodeParameter], [name encodeParameter], score, [secureHash encodeParameter]];

Where encodeParameter is a custom category on NSString that encodes URL parameters and secureHash is a string representing a one way secure hashing of the uid, name, score and some secret known to your iPhone app and your web site. You'll need to figure these out on your own or ask separate questions since this is already getting long.

查看更多
登录 后发表回答