iphone - 网络在模拟器上工作在 iPhone 设备上不起作用

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

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

这使用了 AFNetworking 库,我已经尝试从不同的托管商那里获取文件。

我做错了什么?

最佳答案

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

关于iphone - 网络在模拟器上工作在 iPhone 设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805259/

相关文章:

asp.net - 无法使用 AFNetworking 将 iOS 连接到 asp.net web 服务

objective-c - AFNetworking 错误 URL

android - 在应用程序快速关闭之前混合面板刷新事件

ios - 如果触摸了 UITableViewCell 以外的任何地方,则关闭模式

ios - `AFNetworking` 需要 CocoaPods `Podfile`

iphone - 获取地址簿联系人的组?

iphone - 在 iOS 中使用 MySQL

iphone - 转换后无法访问音频文件

iphone - NSUserDefault 存在 -iPHONE SDK

ios - 我很困惑类扩展中声明的任何方法是否是静态的