ios - NSMutableArray writeToUrl

标签 ios objective-c url nsmutablearray

是否可以使用:

[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"]];

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

最佳答案

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

没有预先构建的方法可以将 NSMutableArray 的内容发送到 URL,但是有一些简单的方法可以自己完成。例如,您可以遍历数组的数据并使用 NSURLRequest创建符合 Web 服务接口(interface)的 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 部分,网络请求的接收者可能需要一些脚本或基础设施才能做出有用的响应。

关于ios - NSMutableArray writeToUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1022714/

相关文章:

url - URL 约定中的新 `#!` 是什么?

iphone - 添加 subview - Monotouch

ios - 带电容器的 Ionic 4 : xcodebuild fails with manual signing process

ios - 了解 NSNetservice 的 “includesPeerToPeer” 属性

ios - iOS中 block (Objective-C)和闭包(Swift)之间的区别

javascript - 检索当前焦点/鼠标悬停的超链接 URL

ios - iPhone 推送通知字数限制

ios - 如何在 SwiftUI 中使用多个选择器更改 UIPickerView 宽度?

ios - 裁剪后无法绘制阴影

java - FileSystemResource : java. io.FileNotFoundException