是否可以使用:
[NSMutableArray writeToURL:(NSString *)path atomically:(BOOL)AuxSomething];为了将文件(NSMutableArray) XML文件发送到url,并更新url以包含该文件?
例如:我有一个数组,我想把它上传到一个特定的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"]];这意味着,我想写一个网址,这是在一个虚拟主机服务(例如batcave.net,网址接收信息,并相应地更新服务器端文件)。像设置这样的高分,用户发送他的分数,服务器更新它的文件,其他用户在运行时下载高分。
发布于 2009-06-21 00:47:33
至于问题的第一部分,我假设您想要使用NSMutableArray的内容来形成某种类型的URL请求(例如,您将发送到web服务并期望返回一些信息的POST……
没有预先构建的方法可以将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...
}为了回答问题的第二部分,web请求的接收者可能需要一些脚本或基础设施来做出有用的响应。
发布于 2009-06-21 05:46:25
https://stackoverflow.com/questions/1022714
复制相似问题