【发布时间】:2011-11-04 04:13:20
【问题描述】:
伙计,这个问题难倒了。尝试使用AFNetworking 下载文件,而我能做的最好的事情就是在 downloadProgressBlock 中捕获进度字节。我已经尝试了许多不同的 AFNetworking 分支,现在我回到了最新版本。看起来有一次 AFHTTPRequestOperation 被修改为也包括 NSURLResponse 但在最新版本中已经消失了。而且,根据下面的代码,永远不会调用“成功”块。我得到下载字节的日志,然后调用 ^complete。永远不会调用成功和错误。
任何关于这方面的指导都会很棒。我无法弄清楚数据返回的位置以及如何让它在其上使用 NSFileManager?我需要下载文件,而不是为图像写流等。
编辑:我还尝试按照文档中的建议通过覆盖 - (void)connection:didReceiveData 来捕获数据,但没有做任何事情。
// 网址为http://mydomain.com/somezip.zip
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", url]]];
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request success:^(id object) {
NSLog(@"hey, some success!");
} failure:^(NSHTTPURLResponse *response, NSError *error) {
NSLog(@"%@", error);
}];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
// callback is sent in and works great. Just outputting to the console at the moment
SEL callme = NSSelectorFromString(callback);
[sender performSelectorOnMainThread:callme withObject:[NSNumber numberWithInt:bytesRead] waitUntilDone:NO];
}];
[operation setCompletionBlock:^{
NSLog(@"operation complete");
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
【问题讨论】:
标签: afnetworking