iosnsurlsessionnsurlsessiondownloadtasknsurlsessionconfiguration

Showing the file download progress with NSURLSessionDataTask


I want to display file download progress (how many bytes are received) of particular file. It works fine with the NSURLSessionDownloadTask .My question is I want to achieve the same with the NSURLSessionDataTask.

Here is the code which receives file into NSData and writes to document folder:

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

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

NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:theRessourcesURL
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{ 
       if(error == nil)
       {

            NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

               NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docsDir, Name];

               NSLog(@"SIZE : %@",[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);

               [data writeToFile:pathToDownloadTo options:NSDataWritingAtomic error:&error];
       }
}];

[dataTask resume];

I am getting file size after write or complete datatask (after the file is received) :

NSLog(@"SIZE : %@",[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);

But i want to display it's current bytes received status, is this possible with NSURLSessionDataTask?


Solution

  • You need to implement following delegates:

    <NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>

    Also need to create two properties:

    @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;
    }