1

I am performing a download through Download Manage class and updating a progress bar view according to the received bytes in the delegate connection didReceiveData.

Everything is working fine when I am on the page where downloading is happening but when I am going to some other view controller and coming back to my downloading page, the connection delegate functions are being called while transition of page but coming back does not upgrade the progress bar.

Thats my code

for (NSInteger row = 0; row < [downloadManager.downloads count]; row++)
{
    if (download == downloadManager.downloads[row])
    {
        currentTime = [NSDate timeIntervalSinceReferenceDate];

        [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row inSection:0] download:download];

lbl_percentage.text = [NSString stringWithFormat:@"%.02f%%",((double) download.progressContentLength / (double) download.expectedContentLength)*100];

        lbl_received_data.text = [NSString stringWithFormat:@"%@ / %@",[self transformedValue:(double) download.progressContentLength],[self transformedValue:(double) download.expectedContentLength]];

        double downloadSpeed = (double) download.progressContentLength / (currentTime - startTime);

        lbl_speed.text = [NSString stringWithFormat:@"%@/sec", [self transformedValue:downloadSpeed]];

        NSLog(@"Download Running");

        break;
    }
}
4

1 回答 1

0
-(void)viewDidAppear:(BOOL)animated
{
    [[self downloadManager] setDelegate:self];
}

-(IBAction)btn_Download_click:(id)sender
{
    if ([self downloadManager]) {
        [[self downloadManager] setDelegate:self];
    }else{
        self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];
        self.downloadManager.maxConcurrentDownloads = 1;
    }

    downloadData = [[NSMutableData data] retain];

    NSLog(@"coverimg%@",coverimg);

    NSString *string=[[NSString stringWithFormat:@"http://www.virtueinfotech.com/moralstory/pdf/%@",coverimg] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *reqURL = [NSURL URLWithString:string];

    startTime = [NSDate timeIntervalSinceReferenceDate];

    [self Readfunction];
    [self.downloadManager addDownloadWithFilename:filePath URL:reqURL];

}
-(IBAction)btn_Read_click:(id)sender
{
    [self Readfunction];

    PdfReadView *pdfRv = [[PdfReadView alloc]init];
    [self.navigationController pushViewController:pdfRv animated:YES];
}

#pragma mark - Download Manager Delegate Methods

- (void)updateProgressViewForIndexPath:(NSIndexPath *)indexPath download:(Download *)download
{
    if (download.expectedContentLength >= 0)
    {
        // Calculate the progress.
        dispatch_async(dispatch_get_main_queue(), ^{

            [self setProgress:(double) download.progressContentLength / (double) download.expectedContentLength animated:YES];

            lbl_percentage.text = [NSString stringWithFormat:@"%.02f%%",((double) download.progressContentLength / (double) download.expectedContentLength)*100];

            lbl_received_data.text = [NSString stringWithFormat:@"%@ / %@",[self transformedValue:(double) download.progressContentLength],[self transformedValue:(double) download.expectedContentLength]];

            double downloadSpeed = (double) download.progressContentLength / (currentTime - startTime);

            lbl_speed.text = [NSString stringWithFormat:@"%@/sec", [self transformedValue:downloadSpeed]];
        });
    }
    else
    {
        [self setProgress:(double) (download.progressContentLength % 1000000L) / 1000000.0 animated:YES];
    }
}
- (void)downloadManager:(DownloadManager *)downloadManager downloadDidFail:(Download *)download;
{
    [self setProgress:0.0f animated:YES];
    UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"Oops !!" message:@"Downloading Failed...." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alt show];
}

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidReceiveData:(Download *)download;
{

    lbl_total_size.text = [NSString stringWithFormat:@"%@",[self transformedValue:(double) download.expectedContentLength]];

    for (NSInteger row = 0; row < [downloadManager.downloads count]; row++)
    {
        if (download == downloadManager.downloads[row])
        {
            currentTime = [NSDate timeIntervalSinceReferenceDate];
            dispatch_async(dispatch_get_main_queue(), ^{

                [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row inSection:0] download:download];
            });
            NSLog(@"Download Running");

            break;
        }
    }
}
于 2015-10-07T12:09:53.427 回答