1

I am using following code to download file from url's asynchronously,

 NSMutableData *responseData = [[NSMutableData alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://www.tuiscos.com/images/trading.png"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request 
                                           queue:[NSOperationQueue mainQueue] 
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                   // do something with data
                                   [responseData appendData:data];
                                   myImage8.image = [UIImage imageWithData:data];
                                   NSInteger len = response.expectedContentLength;
                                   NSInteger receiverdBytes = 0;
                                   receiverdBytes = data.length+ receiverdBytes;
                                   float prog = (float)[responseData length]/(float)len;
                                   [progress8 setProgress:prog];
                               }];

as the download progresses, I want to update the progress bar, but using this code, I am not getting a gradual progress, instead it is waiting to complete the download and jumping to the maximum value. How can I make a gradual progress in the value? Can somebody provide a sample code? For asynchronous method with delegate methods. Thanks :)

Mithuzz
  • 1,091
  • 14
  • 43

2 Answers2

1

If you don't want to code everything on your own, I would suggest using ASIHTTPRequesst on this task:

http://allseeing-i.com/ASIHTTPRequest/How-to-use

It is very simple to implement and you can do simultaneous, asynchrony downloads. It also provides delegates for all needs, also for progress updates.

I used it in my projects for almost a year now and never regretted it.

butters
  • 164
  • 8
0

CompletionHandler is executed at completion, of course. You have to a delegate for the connection. Use -initWithRequest:delegate: method. You will have to code the NSURLConnectionDelegate methods and the one you need to set progressView value is -connection:didReceiveData:

Here is the doc: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.pdf

Gabriel
  • 3,319
  • 1
  • 16
  • 21
  • Thanks for the reply, i want to perform multiple downloads at the same time. So how can i handle it using delegte methods? And also i need to update the progress on different uiprogressviews. Is it possible? – Mithuzz Mar 28 '12 at 15:41
  • 1
    @John You can have all your openned connections in an array and compare them with the connection in the **connection:didReceiveData:** delegate method to identify, which has currently received data – Martin Pilch Mar 29 '12 at 09:21
  • @MartinPilch thanks, but I am new to this area, so can you show me a sample code? – Mithuzz Mar 29 '12 at 09:25
  • @John, if you are new to this area, you'd better read documentation or a good guide. You want to code simultaneous uploads with progress indicators man. That is no sample code but work to be done. I recommend you to learn how to use NSURLConnection objects. It's easy and necessary even if you are to use sample code or public domain code. You have to know what you are doing. – Gabriel Mar 29 '12 at 14:23
  • i went through the documents, but i think all the delegate methods are for synchronous method, am not sure. Or it is common for asynchronous and synchronous methods? – Mithuzz Mar 30 '12 at 05:18
  • delegate methods are for asynchronous url connection requests. – Gabriel Mar 30 '12 at 13:00