网络在模拟器上工作不适用于iPhone设备

时间:2013-02-11 02:57:22

标签: iphone ios afnetworking simulator

此代码在模拟器上完美运行,写出文件。

然而,在iPhone上,它不会写出文件。

DLog(@"");

timeStart  = [NSDate date];


NSURL *url = [NSURL URLWithString:SAT_URL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FILE_NAME];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    DLog(@"updated words to: %@",path);
    //DLog(@"response: %@",responseObject);

    [self processWords];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // Deal with failure
}];

[operation start];

设备上的rawWords和行都为null:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FILE_NAME];
NSString *rawWords = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [rawWords componentsSeparatedByString:@"\n"];

这使用AFNetworking lib,我尝试从不同的主机提取文件。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

你不能写一个包(绝对不能写在设备上,也许在模拟器中可能)。您可以写入一些预定义的目录(缓存,文档)。查看StackOverflow上的以下帖子:NSSearchPathForDirectoriesInDomains explanation confused