OStack程序员社区-中国程序员成长平台

标题: ios - 使用 NSURLSession 下载多个文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:43
标题: ios - 使用 NSURLSession 下载多个文件

我想从服务器的url数组中一个一个下载多个文件。

我需要显示每个下载文件的进度,用户也可以取消它。

ViewControllerh:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIView *vw_download;
    IBOutlet UILabel *lbl_BookTitle;
    IBOutlet UILabel *lbl_BookDes;
    IBOutlet UILabel *lbl_downloadStatus;
    IBOutlet UIImageView *img_todownload;
    IBOutlet UIProgressView *vw_downloadbar;
    IBOutlet UIButton *btn_stopDownload;
}

-(IBAction)btn_stopDownloadingid)sender;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDelegate> {
    NSMutableArray *downloadLinksArray;
    NSURLSession *session ;
    NSURLSessionDownloadTask *task;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    downloadLinksArray=[[NSMutableArray alloc]initWithObjects"URL1",@"URL2", @"URL3", @"URL4",  nil];

    [vw_downloadbar setProgress:0];
    lbl_downloadStatus.text=[NSString stringWithFormat"Downloading in Progress"];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)startDownloadid)sender {
    //[self startChapterDownLoad];

    for (int i=0;i< [downloadLinksArray count];i++) {
        NSURL *Url= [NSURL URLWithString:[NSString stringWithFormat"%@",[downloadLinksArray objectAtIndex:i]]];

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

        session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];

        NSURLRequest* request = [NSURLRequest requestWithURL:Url];

        task = [session downloadTaskWithRequest:request];

        [task resume];
    }
}

-(void)URLSessionNSURLSession *)session downloadTaskNSURLSessionDownloadTask *)downloadTask didWriteDataint64_t)bytesWritten totalBytesWrittenint64_t)totalBytesWritten totalBytesExpectedToWriteint64_t)totalBytesExpectedToWrite {
    CGFloat percentDone = (double)totalBytesWritten/(double)totalBytesExpectedToWrite;
    // Notify user.

    [self performSelectorOnMainThreadselector(updateProgress withObject:[NSNumber numberWithFloat:percentDone] waitUntilDone:NO];
}

- (void) updateProgressNSNumber *)percent {
    [vw_downloadbar setProgress:percent.floatValue];
}

-(void)URLSessionNSURLSession *)session downloadTaskNSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    // Either move the data from the location to a permanent location, or do something with the data at that location.
}

-(IBAction)btn_stopDownloading:(id)sender {
    [task cancel];
    [vw_downloadbar setProgress:0];
}



Best Answer-推荐答案


如果你想下载多个文件并且当它完成你想通知:

请检查代码 - 使用 NSFileManager :

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession              *session       = [NSURLSession sessionWithConfiguration:configuration];

NSString      *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSFileManager *fileManager   = [NSFileManager defaultManager];

for (NSString *filename in self.filenames)
{
    NSURL *url = [baseURL URLByAppendingPathComponent:filename];
    NSURLSessionTask *downloadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

        NSString *finalPath = [documentsPath stringByAppendingPathComponent:filename];

        BOOL success;
        NSError *fileManagerError;
        if ([fileManager fileExistsAtPath:finalPath]) {
            success = [fileManager removeItemAtPath:finalPath error:&fileManagerError];
            NSAssert(success, @"removeItemAtPath error: %@", fileManagerError);
        }

        success = [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:finalPath] error:&fileManagerError];
        NSAssert(success, @"moveItemAtURL error: %@", fileManagerError);

        NSLog(@"finished %@", filename);
    }];
    [downloadTask resume];
}

你可以选择:NSOperationQueue

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 4;

NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self methodToCallOnCompletion];
    }];
}];

for (NSURL* url in urlArray)
{
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        NSString *filename = [documentsPath stringByAppendingString:[url lastPathComponent]];
        [data writeToFile:filename atomically:YES];
    }];
    [completionOperation addDependencyperation];
}

[queue addOperations:completionOperation.dependencies waitUntilFinished:NO];
[queue addOperation:completionOperation];

它将显示下载文件的百分比:

您可以根据您的要求进行更改:(我没有测试过,但它会起作用)。

@property (nonatomic, retain) NSMutableData *dataToDownload;
@property (nonatomic) float downloadSize;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

    NSURL *url = [NSURL URLWithString: @"your url"];
    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithURL: url];

    [dataTask resume];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    completionHandler(NSURLSessionResponseAllow);

    progressBar.progress=0.0f;
    _downloadSize=[response expectedContentLength];
    _dataToDownload=[[NSMutableData alloc]init];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    [_dataToDownload appendData:data];
    progressBar.progress=[ _dataToDownload length ]/_downloadSize;
}

关于ios - 使用 NSURLSession 下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245369/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4