1

I am using following code for downloading epub /pdf from a url .I like to give a progress bar so when I start downloading it shows the progress and when the download completes it will popup a message. How can I implement this?

My code for downloading file

-(void)Download
 {
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL    URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];

    //Store the Data locally as epub  File if u want pdf change the file extension  

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];

    [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
 }

I am using this codes in my .m file but it's not working for me

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _totalFileSize = response.expectedContentLength;
    responseData = [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    _receivedDataBytes += [data length];
    MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
    [responseData appendData:data];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Naveen
  • 1,251
  • 9
  • 20
  • 38
  • http://stackoverflow.com/a/14313172/1228669 may be this answer related to your question – prasad Apr 18 '13 at 12:24
  • possible duplicate of [Add UIProgressView to a NSURLConnection?](http://stackoverflow.com/questions/8382075/add-uiprogressview-to-a-nsurlconnection) – ldav1s Apr 18 '13 at 22:41

3 Answers3

2

use NSURLConnection

in .h file

double datalength;
NSMutableData *databuffer;
UIProgressView *progress;

in .m file

-(void)Download
{
      NSURLConnection *con=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]] delegate:self startImmediately:YES];
      [con start];
}

delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    datalength = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [databuffer appendData:data];
    progress.progress = (databuffer.length/datalength);
    self.HUD.detailsLabelText = [NSString stringWithFormat:@"Downloading  %.f  %%",(databuffer.length/datalength)*100];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];
    [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
}
Manu
  • 4,730
  • 2
  • 20
  • 45
0

If you do curl -vvv -o epub.pdf http://www.feedbooks.com/book/3471.epub you will see the following line:

Content-Length: 603244

The content-length header is the size in bytes of the data you are downloading. You can use that to track your progress as you write your data.

With your current code, you can't really do what you want though. You should check out this answer for more info.

Community
  • 1
  • 1
NG.
  • 22,560
  • 5
  • 55
  • 61
0

You can check data length of NSData. Then you will find actual downloaded data.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
    // append the new data to the receivedData

    [receivedData appendData:data];
}

Here you will get length of data in bytes. You can convert it as per your need.

It may help you.

Nirav Jain
  • 5,088
  • 5
  • 40
  • 61