5

I created a UIProgressView. But i used NSTimer to UIProgressView's process . Now I need to integrate UIProgressView process, when URL is loading. UIProgressView's size will be depends upon the NSURLConnection's data.

I used the following code to NSURLConnection.

-(void)load {
    NSURL *myURL = [NSURL URLWithString:@"http://feeds.epicurious.com/newrecipes"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

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


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];

    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"Warning"];
    [alert setMessage:@"Network Connection Failed?"];
    [alert setDelegate:self];
    [alert addButtonWithTitle:@"Yes"];

    [alert show];
    [alert release];

    NSLog(@"Error");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    responsetext = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Velmurugan
  • 2,303
  • 6
  • 30
  • 45

1 Answers1

15

In your didReceiveResponse function you could get the total filesize like so: _totalFileSize = response.expectedContentLength;.

In your didReceiveData function you can then add up to a total bytes received counter: _receivedDataBytes += [data length];

Now in order to set the progressbar to the correct size you can simply do: MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize

(either in the didReceiveData function or somewhere else in your code)

Don't forget to add the variables that hold the number of bytes to your class!

I hope this helps..

EDIT: Here's how you could implement the delegates in order to update progressview

-(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];
 }
Hless
  • 3,326
  • 20
  • 22
  • How to declare response in didReceiveResponse , please edit the following code. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { totalFileSize=response.expectedContentLength; receivedDataBytes += [data length]; NSLog(@"totalFileSize / totalFileSize = %f",receivedDataBytes / totalFileSize); [responseData appendData:data]; } – Velmurugan Nov 23 '10 at 11:45
  • didReceiveResponse and didReceiveData are 2 different delegates. It should be: - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { totalFileSize=response.expectedContentLength; } and remove that line from didReceiveData – Hless Nov 23 '10 at 12:07
  • Edited my answer. I forgot to mention that one of the values you divide with should be casted to a float. Since you could otherwise end up with an integer, without float precision. – Hless Nov 23 '10 at 12:24
  • B_vdl : Thanks for your answer. my problem is solved. but i edit some code. please check and tell your command... – Velmurugan Nov 23 '10 at 12:48
  • 1
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { totalFileSize = response.expectedContentLength; responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { receivedDataBytes += [data length]; NSLog(@"receivedDataBytes / (float)totalFileSize; = %2f",(1+((float)totalFileSize / (float)receivedDataBytes)*1000)); // MyProgressBar.progress = receivedDataBytes / (float)totalFileSize; [responseData appendData:data]; } – Velmurugan Nov 23 '10 at 12:49
  • I get output as following totalFileSize / receivedDataBytes; = 0.594320 totalFileSize / receivedDataBytes; = 0.836173 totalFileSize / receivedDataBytes; = 0.915154 totalFileSize / receivedDataBytes; = 0.948283 totalFileSize / receivedDataBytes; = 0.959876 totalFileSize / receivedDataBytes; = 0.973168 totalFileSize / receivedDataBytes; = 0.978955 totalFileSize / receivedDataBytes; = 0.982887 totalFileSize / receivedDataBytes; = 0.984343 totalFileSize / receivedDataBytes; = 0.985732 totalFileSize / receivedDataBytes; = 0.987008 totalFileSize / receivedDataBytes; = 0.988136 – Velmurugan Nov 23 '10 at 13:10
  • totalFileSize / receivedDataBytes; = 0.988829 totalFileSize / receivedDataBytes; = 0.989402 totalFileSize / receivedDataBytes; = 0.990069 totalFileSize / receivedDataBytes; = 0.990952 totalFileSize / receivedDataBytes; = 0.991410 totalFileSize / receivedDataBytes; = 0.991799 totalFileSize / receivedDataBytes; = 0.992157 totalFileSize / receivedDataBytes; = 0.992551 totalFileSize / receivedDataBytes; = 0.993423 totalFileSize / receivedDataBytes; = 0.993597 – Velmurugan Nov 23 '10 at 13:11
  • See the reference (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIProgressView_Class/Reference/Reference.html) the progress property expects a value between 0.0 and 1.0 where 1.0 indicates completion of the task. Thus setting this property with the values recieved from the division should do the trick! – Hless Nov 23 '10 at 13:21
  • i also got the values between 0.0 to 1.0. But i used the above calculation.. My problem is solved. But i dont know my code is correct or wrong. But it works well. if any mistake in my calculation please tell me. – Velmurugan Nov 23 '10 at 17:57
  • The code is allright. But there are certain (offtopic) ways to optimalize. You access the progressview every time you receive data. You could add a timer somewhere else in the code to minimalize the times the progressview object is accessed. Furthermore (totally offtopic), you currently add received data to an object in memory. This is fine with small files. However if you download bigger files (over 20 MB) you might experience some severe memory problems. Workaround is to save directly to disk. I will now stop answering to this thread before we get even more offtopic :). – Hless Nov 24 '10 at 09:50