iOS Asihttprequest自定义进度跟踪

时间:2012-05-03 14:59:16

标签: ios asihttprequest

我很想用AsiHttpRequest显示下载的进度,如下所示:  300 kb / 5 Mb并不断更新第一个值。 我明白如果我使用 - (void)请求:(ASIHTTPRequest *)请求didReceiveData:(NSData *)数据我需要自己管理nsdata对象并自己写下载文件

我试过这个,但不行! 可变数据为空! 我使用带有ARC的iOS 5。 谢谢你的帮助!

已更新

我用这个代码解决了,我已经将委托设置为自己,并且每件事情都正常工作。 只是我不明白,如果以这种方式和ARC我需要清除代表

- (void)dealloc
{
   [request clearDelegatesAndCancel];
}

感谢大家的帮助!!

已更新

-(void)downloadFile: (NSString*) file {
           NSString* urlFilePdf = [NSString stringWithFormat:@"http://www.mywebsite.com/%@",file];

            myFileName = file;

            NSURL *url = [NSURL URLWithString:urlFilePdf];

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:file];


            progressAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DOWNLOADFILE",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

            progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
            [progressAlert addSubview:progressView];
            [progressView setProgressViewStyle: UIProgressViewStyleDefault];

            lblTotal = [[UILabel alloc] initWithFrame:CGRectMake(5, 50, 273, 20)];
            [lblTotal setTextAlignment:UITextAlignmentCenter];
            [lblTotal setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0]];
            [lblTotal setTextColor:[UIColor whiteColor]];
            [lblTotal setBackgroundColor:[UIColor clearColor]];
            [progressAlert addSubview:lblTotal];

            requestFile = [ASIHTTPRequest requestWithURL:url];
            [requestFile setDelegate:self];
            [requestFile setDownloadDestinationPath:filePath];
            [requestFile setDownloadProgressDelegate:self];
            [requestFile setShowAccurateProgress:YES];    
            [progressAlert show];

            [requestFile startAsynchronous];

}

- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength
{
    NSLog(@"%@",[NSString stringWithFormat:@"incrementDownloadSizeBy %quKb", newLength/1024]);
}

- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
    NSLog(@"%@",[NSString stringWithFormat:@"didReceiveBytes %quKb", bytes/1024]);
    currentLength += bytes;
    [lblTotal setText:[NSString    stringWithFormat:@"%quKb/%@",currentLength/1024,self.TotalFileDimension]];
}

- (void)setProgress:(float)newProgress {
    NSLog(@"%f",newProgress);
    progressView.progress = newProgress;
}


-(void) requestFinished:(ASIHTTPRequest *)request
{
    // code ...
}

-(void) requestFailed:(ASIHTTPRequest *)request
{
    // code ...
}

- (void)dealloc
{
   [request clearDelegatesAndCancel];
}

1 个答案:

答案 0 :(得分:0)

您应该实施request:incrementDownloadSizeBy:方法。

请注意:您撰写的问题的标题是 iOS Asihttprequest自定义进度跟踪。如果您查看the ASIHTTPRequest documentation,您会看到一个标题,准确描述了您的要求 - Custom progress tracking

如果你没有看到这个,那那就是一个很大的问题。在向人们寻求帮助之前,您应该始终查看文档。