【问题标题】:networking works on simulator does not work on iPhone device网络在模拟器上工作不适用于 iPhone 设备
【发布时间】:2013-02-11 02:57:22
【问题描述】:

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

但是,在 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 和 lines 在设备上都是空的:

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

这使用了 AFNetworking 库,我尝试从不同的主机获取文件。

我做错了什么?

【问题讨论】:

  • 设备是否连接到互联网?只是检查。

标签: iphone ios afnetworking simulator


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多