我triyng用AsiHttpRequest顯示下載進度如下: 300 kb/5 Mb並不斷更新第一個值。我知道,如果我使用 - (void)請求:(ASIHTTPRequest *)請求didReceiveData:(NSData *)數據我需要自己管理nsdata對象,並自己寫下載的文件iOS Asihttprequest自定義進度跟蹤
我試過了但不工作! 可變數據爲空! 我使用iOS 5與ARC。 感謝您的幫助!
修訂
我解決了這個代碼,我已經設置委託給自己和所有的東西正常工作。 只是如果以這種方式與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];
}
據我所知,當您決定手動保存數據時,您還必須通過檢查已收到的數據的長度來手動處理進度。至於你如何確定最終的長度......不確定。 –
謝謝Jacob,我假設appendData一步一步地存儲下載的數據。 - (void)請求:(ASIHTTPRequest *)請求didReceiveData:(NSData *)數據被不斷調用,並且如果我記錄它,(NSData *)數據的大小會隨着方式而改變。但最後收到的數據爲空 –