【发布时间】:2014-02-15 16:06:26
【问题描述】:
我正在使用 AFNetworking 下载一个 mp3 文件。下载发生在特定的 ViewController 中。由于 mp3 文件很大,所以需要几分钟才能完成下载。问题是当我转到另一个 ViewController 时,下载停止,我必须留在下载 ViewController 上并等待下载完成。这会让用户感到沮丧!即使下载 ViewController 被解除,有没有办法让下载继续进行? 这是我用来下载的代码:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sample.com/samplefile.mp3"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *mp3Name = @"sample.mp3";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:mp3Name];
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);
}];
【问题讨论】:
标签: ios iphone nsurlconnection afnetworking nsoutputstream