6

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

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

我做了一些搜索,在 SO 上遇到了一些问题,建议我使用:

[_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:failure:'

我需要使用更新的线路吗???

4

2 回答 2

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];
于 2013-01-04T06:47:50.283 回答
4

是的,请确保您使用了正确的路径NSOutputStream

添加这个:

[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];
[_operation start];
于 2013-01-04T06:47:54.573 回答