I am trying to create an iPhone app as in the fig below:
In this i am dynamically adding some imageviews dynamically into a uiscroll view. Each imageview contains a UIButton and UIProgressView. When click on each it should different urls and the loading of each url should appear on the corresponding UIProgressView. I am using asynchronous method and it should load multiple progress views at the same time. Here is the code i have used,
- (void)startDownload:(id)sender {
UIButton *btn = (UIButton *)sender;
int btnTag = btn.tag;
selectedTag = btn.tag;
for (int x=0; x < [urlArray count]; x++) {
if (btnTag == x) {
NSURL *url = [NSURL URLWithString:[urlArray objectAtIndex:x]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"Expected length: %lld ",response.expectedContentLength);
}];
if (theConnection) {
receivedData = [NSMutableData data];
}else {
NSLog(@"Connection failed");
}
}
}
}
- (void)makeMyProgressMove{
UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag];
UIProgressView *currentProgress = (UIProgressView *)[image viewWithTag:selectedTag];
NSLog(@"Prog tag: %d",currentProgress.tag);
if(currentProgress)
{
float actualProgress = (_receivedDataBytes / (float)_totalFileSize);
currentProgress.progress = actualProgress;
} else {
NSLog( @"couldn't get the progress view for the image with tag: %d", selectedTag );
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_totalFileSize = response.expectedContentLength;
receivedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
_receivedDataBytes += [data length];
NSLog(@"Receiving data: %2f", _receivedDataBytes / (float)_totalFileSize);
if ((_receivedDataBytes / (float)_totalFileSize) < 1) {
[self performSelectorOnMainThread: @selector(makeMyProgressMove) withObject: NULL waitUntilDone:NO];
}
else if ((_receivedDataBytes / (float)_totalFileSize) == 1){
[self performSelectorOnMainThread: @selector(makeMyProgressMove) withObject: NULL waitUntilDone:NO];
_receivedDataBytes = 0;
_totalFileSize = 0;
}
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
}
But its not working. Any idea?
allocate each UIWebView with different url and and addSubView UIActivityIndicatorView to every UIWebView.Give tag for each UIActivityIndicator ..
-(void)action {
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc]initWithFrame:frame];//give y value incremented frame.
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.center = webView.center;
av.tag = INDICATOR_TAG;
[webView addSubview:av];
webView.delegate = self;
[av startAnimating];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
UIActivityIndicatorView *indicator = [webView viewWithTag:INDICATOR_TAG];
indicator.hidden = YES;
}
call this action{} method for showing multiple times.
UIWebView doesn't give you any progress information in the normal mode. What you need to do is first fetch your data asynchronously using an NSURLConnection. When the NSURLConnection delegate method connection:didReceiveResponse, you're going to take the number you get from expectedContentLength and use that as your max value. Then, inside the delegate method connection:didReceiveData, you're going to use the length property of the NSData instance to tell you how far along you are, so your progress fraction will be length / maxLength , normalized to between 0 and 1.
Finally, you're going to init the webview with data instead of a URL (in your connection:didFinishLoading delegate method).
Reference : How to use UIProgressView while loading of a UIWebView?