Friday, September 7, 2012

UIProgressBar download file

Helpful example of creating your own progress bar download.  Utilizes connection:(NSURLConnection *) delegate methods:


http://www.developers-life.com/progress-bar-download-file-on-iphone.html

Critical components:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.receivedData appendData:data];
    
    NSInteger receivedLen = [data length];
    bytesReceived = (bytesReceived + receivedLen);
    
    if(expectedBytes != NSURLResponseUnknownLength) {
        self.progress = ((bytesReceived/(float)expectedBytes)*100)/100;
        percentComplete = self.progress*100;
    }
    
    [delegate downloadBarUpdated:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.delegate downloadBar:self didFailWithError:error];
    [connection release];
}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.delegate downloadBar:self didFinishWithData:self.receivedData suggestedFilename:localFilename];
    [connection release];
}

No comments:

Post a Comment