指一个下面的代码。这是使用AFNetworking代码下载的一些文件。
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blahblah.com/blahblah.mp3"]];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [paths objectAtIndex:0] stringByAppendingPathComponent:@"blahblah.mp3"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
myProgressView.progress = (float)totalBytesRead/totalBytesExpectedToRead;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"downloadComplete!");
}];
[operation start];
谢谢,我忽略了这一点。 – 2012-08-16 10:58:03
给定的URL不起作用。 [This](http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFURLConnectionOperation.html)是网址。 – esh 2013-06-21 18:48:32
谢谢,他们已经改变了他们的文档链接,因为我发布了(当时Cocoadocs不存在)。我已经修改了答案。 – lxt 2013-06-21 18:54:48