NSMutableArray writeToUrl

独自空忆成欢 提交于 2019-12-11 15:05:29

问题


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.


回答1:


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.




回答2:


Here, Answer in this question:
Creating a highscore like system, iPhone side

I couldn't edit my post because I posted from my iPhone as an anonymous user, sorry.



来源:https://stackoverflow.com/questions/1022714/nsmutablearray-writetourl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!