Ios – NSMutableArray writeToUrl

iosnsmutablearrayobjective curl

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.

Best Answer

As for part one of your question, I'll assume you want to use the contents of a NSMutableArray to form some sort of a URL request (like POST) that you will send to your web service and expect back some information...

There is no prebuilt way of sending the contents of a NSMutableArray to an URL but there are simple ways of doing this yourself. For example, you can loop through the data of your array and make use of NSURLRequest to create a URL request that complies with the interface of your web service. Once you've constructed your request you can send it by passing it a NSURLConnection object.

Consider this very simple and incomplete example of what the client-side code might look like using an Obj-C array to provide data...

NSMutableData *dataReceived; // Assume exists and is initialized
NSURLConnection *myConnection;

- (void)startRequest{
    NSLog(@"Start");

    NSString *baseURLAddress = @"http://en.wikipedia.org/wiki/";

    // This is the array we'll use to help make the URL request
    NSArray *names = [NSArray arrayWithObjects: @"Jonny_Appleseed",nil];
    NSString *completeURLAsString = [baseURLAddress stringByAppendingString: [names objectAtIndex:0]];

    //NSURLRequest needs a NSURL Object
    NSURL *completeURL = [NSURL URLWithString: completeURLAsString];

    NSURLRequest *myURLRequest = [NSURLRequest requestWithURL: completeURL];

    // self is the delegate, this means that this object will hanlde
    // call-backs as the data transmission from the web server progresses
    myConnection = [[NSURLConnection alloc] initWithRequest:myURLRequest delegate: self startImmediately:YES];
}

// This is called automatically when there is new data from the web server,
// we collect the server response and save it
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"Got some");
    [dataReceived appendData: data];
}

// This is called automatically when transmission of data is complete
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // You now have whatever the server sent...
}

To tackle part 2 of your question, the receiver of a web request will likely require some scripting or infrastructure to make a useful response.

Related Topic