这个问题已经在这里有一个答案:
- 如何从服务器下载视频文件到iPhone时显示进度条? 1个回答
我试图把正在发生的下载过程中同步的进度条。 我的应用程序现在可以用这个代码利用下载文件...
pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://webaddress.com/pro/download/file.pdf"]];
NSString *resourcePDFPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
pdfFilePath = [resourcePDFPath stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:pdfFilePath atomically:YES];
在此代码下载过程中应用程序停止的过程中,是否正常? 现在,我要的是过程中停止时间把一个进度条,同时下载。
我试图寻找到我在网上找到的代码,但我有点困惑,我想我需要一步一步的井解释的参考。
使用AFNetworking ,
这里正在进行的是UIProgressview
#import <AFNetworking/AFNetworking.h>//add to the header of class
-(void)downloadShowingProgress
{
progress.progress = 0.0;
currentURL=@"http://www.selab.isti.cnr.it/ws-mate/example.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MY_FILENAME_WITH_EXTENTION.pdf"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSUInteger totalBytesRead, NSUInteger totalBytesExpectedToRead) {
progress.progress = (float)totalBytesRead / totalBytesExpectedToRead;
}];
[operation setCompletionBlock:^{
NSLog(@"downloadComplete!");
}];
[operation start];
}
使用NSURLConnection的
-(void)downloadWithNsurlconnection
{
NSURL *url = [NSURL URLWithString:currentURL];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
progress.hidden = NO;
[receivedData setLength:0];
expectedBytes = [response expectedContentLength];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
float progressive = (float)[receivedData length] / (float)expectedBytes;
[progress setProgress:progressive];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse: (NSCachedURLResponse *)cachedResponse {
return nil;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[currentURL stringByAppendingString:@".mp3"]];
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[receivedData writeToFile:pdfPath atomically:YES];
progress.hidden = YES;
}
使用ASIHTTPRequest.h类和ASINetworkQueue.h下载该文件。
并用进度条验证码
request = [ASIHTTPRequest requestWithURL:@"http://webaddress.com/pro/download/file.pdf];
[request setDelegate:self];
[request setDownloadProgressDelegate:progressView];
[request setShowAccurateProgress:YES];
request.shouldContinueWhenAppEntersBackground=YES;
request.allowResumeForFileDownloads=YES;
[request startAsynchronous];
这可以帮助你
恐怕这是不正常的,使用异步方法获取的NSData。
首先你应该清楚是否作出同步调用或异步的。 对于移动应用或任何其它应用程序异步优选一个。
一旦你明确的使用NSURLConnection的类来从URL中的数据。 这里是很好的教程 。
而对于加载,你可以在启动请求启动进度和停止它,当你收到connection:didFailWithError:
或connectionDidFinishLoading:
委托方法。