如何在AFNetworking 3中下载带有流媒体的大文件

我想使用AFNetworking 3下载大文件。但我需要恢复下载,同时有任何中断,如互联网丢失或任何其他事情。 如果下载时有任何中断,我想从早点停止下载。 可以使用AFNetworking还是使用其他任何库? 请有人帮助我。 这是我的代码。

NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL]; //Watch the manager to see how much of the file it's downloaded [manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { //Convert totalBytesWritten and totalBytesExpectedToWrite into floats so that percentageCompleted doesn't get rounded to the nearest integer CGFloat written = totalBytesWritten; CGFloat total = totalBytesExpectedToWrite; CGFloat percentageCompleted = written/total; //Return the completed progress so we can display it somewhere else in app //progressBlock(percentageCompleted); NSLog(@"Percentage Completed : %f",percentageCompleted); [self updateProgressBar:percentageCompleted]; }]; NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { //Getting the path of the document directory NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"3511_1464694276.zip"]; //If we already have a video file saved, remove it from the phone return fullURL; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { if (!error) { //If there's no error, return the completion block //completionBlock(filePath); } else { //Otherwise return the error block //errorBlock(error); } }]; [downloadTask resume]; 

在这里阅读更多相关信息: https : //github.com/AFNetworking/AFNetworking

当你编写一些代码或尝试自己的东西时,尝试提问。

下载包含进度的文件:

 - (IBAction)downloadAudio:(id)sender { NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://101songs.com/fileDownload/Songs/0/26958.mp3"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { dispatch_async(dispatch_get_main_queue(), ^{ //Update the progress view [_myProgressView setProgress:downloadProgress.fractionCompleted]; }); } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { // Do operation after download is complete }]; [downloadTask resume]; }