Sishu
Sishu

Reputation: 1558

How to download multiples file in background and update corresponding progressview

I am using NSURLSession to download files in background ..I have seen this

link and it is working fine .But i have to download multiple files ,Like the image i uploaded.

How to achieve this ,any advise would be highly appreciated.

enter image description here

Upvotes: 1

Views: 114

Answers (1)

Sunny Shah
Sunny Shah

Reputation: 13020

You can use Block Coding

  typedef void (^onDownload)(NSData *data);
typedef void (^onDownloadProgress)(id,id);

@property (nonatomic,retain) onDownload block;
@property (nonatomic,retain) onDownloadProgress progressBlock;

Downloading

 -(void) downloadFileFromURL:(NSURL *)url1 withCompletionBlock:(onDownload)completeBlock withProgressBlock:(onDownloadProgress) pBlock{
    self.block = [completeBlock copy];
    self.progressBlock = [pBlock copy];
    self.url = url1;
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:url1] delegate:self];
    [connection start];
}

 -(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
            float progress = (totalBytesWritten *100)/totalBytesExpectedToWrite;
             self.progressBlock(self,[NSNumber numberWithDouble:progress]);
            //return process
    }

Request URL

FileDownloader *download=[[FileDownloader alloc]init];

    [download downloadFileFromURL:url withCompletionBlock:^(NSData *data) {
        [lblProcess setHidden:YES];

      //Compale Dwonloading

    } withProgressBlock:^(FileDownloader *download,NSNumber* process) {
        lblProcess.text=[NSString stringWithFormat:@"%.0f%%",[process doubleValue]];
    }];

Upvotes: 1

Related Questions