目标C:使用进度条下载文件
我正试图把下载过程中进行同步的进度条。 我的应用程序现在可以使用此代码下载文件…
pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"pro/download/file.html"]]; NSString *resourcePDFPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; pdfFilePath = [resourcePDFPath stringByAppendingPathComponent:@"myPDF.pdf"]; [pdfData writeToFile:pdfFilePath atomically:YES];
在此代码的过程中,应用程序在下载过程中停止,这是正常的吗? 现在我想要的是在下载的时候在停止时间放置一个进度条。
我试着查看我在网上find的代码,但是我有点困惑,我想我需要一个一步一步解释的参考。
使用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:@"pro/download/file.html]; [request setDelegate:self]; [request setDownloadProgressDelegate:progressView]; [request setShowAccurateProgress:YES]; request.shouldContinueWhenAppEntersBackground=YES; request.allowResumeForFileDownloads=YES; [request startAsynchronous];
这可能会帮助你
恐怕这是不正常的,使用asynchronous方法来获取NSData。
首先,你应该清楚是否进行同步调用或asynchronous。 对于移动应用程序或任何其他应用程序asynchronous是首选。
清除之后,使用NSURLConnection类从URL中获取数据。 这是一个很好的教程 。
对于加载,您可以在开始请求时启动进度,并在您收到connection:didFailWithError:
时停止它connection:didFailWithError:
或connectionDidFinishLoading:
delegate方法。