NSMutableArray writeToUrl

时间:2009-06-20 22:30:59

标签: ios objective-c url nsmutablearray

是否可以使用:

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

为了将文件(NSMutableArray)XML文件发送到网址,并更新网址以包含该文件?

例如: 我有一个数组,我想将其上传到特定的URL,下次启动应用程序时我想下载该数组。

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

[arrayToWrite writeToURL:

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

在运行时:

NSMutableArray *arrayToRead = 

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

意思是,我想将一个NSMutableArray写入URL,该URL位于Web托管服务上(例如batcave.net,URL接收信息并相应地更新服务器端文件。 像设置这样的高分,用户发送他的分数,服务器更新它的文件,其他用户在运行时下载高分。

2 个答案:

答案 0 :(得分:1)

至于你提问的第一部分, 我假设您要使用NSMutableArray的内容来形成某种URL请求(如POST),您将发送到您的Web服务并期望返回一些信息......

没有预先建立的方式将NSMutableArray的内容发送到URL,但有一些简单的方法可以自己完成。例如,您可以循环遍历数组的数据,并使用NSURLRequest创建符合Web服务接口的URL请求。构建完请求后,您可以通过传递NSURLConnection对象来发送请求。

考虑这个非常简单且不完整的示例,说明使用Obj-C数组提供数据的客户端代码可能是什么......

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...
}

要解决问题的第2部分,Web请求的接收者可能需要一些脚本或基础结构才能做出有用的响应。

答案 1 :(得分:1)

在这里,回答这个问题:
Creating a highscore like system, iPhone side

我无法编辑我的帖子,因为我是以匿名用户身份从我的iPhone发布的,抱歉。