Update iPhone UIProgressView during NSURLConnection download.

Posted by Scott Pendleton on Stack Overflow See other posts from Stack Overflow or by Scott Pendleton
Published on 2010-03-26T03:47:43Z Indexed on 2010/03/26 3:53 UTC
Read the original article Hit count: 770

I am using this code:

NSURLConnection *oConnection=[[NSURLConnection alloc] initWithRequest:oRequest delegate:self];

to download a file, and I want to update a progress bar on a subview that I load. For that, I use this code:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [oReceivedData appendData:data];
 float n = oReceivedData.length;
 float d = self.iTotalSize;
 NSNumber *oNum = [NSNumber numberWithFloat:n/d];
 self.oDPVC.oProgress.progress = [oNum floatValue];
}

The subview is oDPVC, and the progress bar on it is oProgress. Setting the progress property does not update the control.

From what I have read on the Web, lots of people want to do this and yet there is not a complete, reliable sample. Also, there is much contradictory advice. Some people think that you don't need a separate thread. Some say you need a background thread for the progress update. Some say no, do other things in the background and update the progress on the main thread.

I've tried all the advice, and none of it works for me.

One more thing, maybe this is a clue. I use this code to load the subview during applicationDidFinishLaunching:

self.oDPVC = [[DownloadProgressViewController alloc] initWithNibName:@"DownloadProgressViewController" bundle:nil];
[window addSubview:self.oDPVC.view];

In the XIB file (which I have examined in both Interface Builder and in a text editor) the progress bar is 280 pixels wide. But when the view opens, it has somehow been adjusted to maybe half that width. Also, the background image of the view is default.png. Instead of appearing right on top of the default image, it is shoved up about 10 pixels, leaving a white bar across the bottom of the screen.

Maybe that's a separate issue, maybe not.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiprogressview