views:

1225

answers:

2

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.

A: 

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.

Peter N Lewis
That is what I'm interested in, I think there is no problem using the writeToURL method, The problem is scripting, I need some scripting explanation if it's PHP or ASP so I can handle data that is sent to the URL.
natanavra
Well, there is a problem using the writeToURL method in that it only works with file:// URLs so it will never access a web site (well, except in so much as you could mount a web site as a disk).
Peter N Lewis
Hmmm, then how do people create highscore servers? could you please elaborate 'cause this is what I'm actually looking for, I want to receive data from an iPhone and update a webpage accordingly and then other iPhone users can download the info using (initWithContentsOfURL:).MY BASIC QUESTION IS: How do I create a highscore like system.
natanavra
In that case, I suggest you either heavily edit your question (especially the title) or make a new question, since you cannot do what you want with writeToURL. For future reference, you'll get better results by asking the question you really want answered (and including details about what you've tried) that asking questions about what you're trying only.
Peter N Lewis
A: 

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.

Peter N Lewis