AFNetworking - 保存下载的文件

时间:2013-01-04 06:42:36

标签: objective-c ios download afnetworking

我正在使用AFNetworking并且可以成功下载文件。

在下载结束时,它不会出现在我设置的目录中。

我做了一些搜索,并在这里发现了一些问题,建议我使用:

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

但是这出现了一个错误,据我所知,在他们的文档中没有提到。

错误是:

  

/ Users / Jeff / Documents / Dropbox-01 / Dropbox / Xcode Projects / Try Outs - JEFF / testDownload / testDownload / JWKDownloadViewController.m:177:10:'AFURLConnectionOperation'没有可见的@interface声明选择器'setCompletionBlockWithSuccess:故障:

我需要使用更新的行???

2 个答案:

答案 0 :(得分:8)

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"..."]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"filename"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation start];

答案 1 :(得分:4)

是确保您使用了NSOutputStream

的正确路径

添加:

[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];
[_operation start];