这是我使用NSUrlConnection下载pdf文件并将其下载到文档目录的代码。
-(void)startDownloadFile:(UIButton *)sender
{
ResourceTelephones * resource = [array objectAtIndex:sender.tag];
NSString * currentURL = resource.website;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
NSString * filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"myPDF%ld.pdf",(long)sender.tag]];
NSLog(@"PDF path: %@",filePath);
[data writeToFile:filePath atomically:YES];
});
}
正在使用以下代码查看文件
-(void)viewAction:(UIButton *)sender
{
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"myPDF%ld.pdf",(long)sender.tag]]];
if (url) {
// Initialize Document Interaction Controller
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
// Configure Document Interaction Controller
[_documentInteractionController setDelegate:self];
// Preview PDF
[_documentInteractionController presentPreviewAnimated:YES];
}
}
现在我想把每个单元格的进度条对应于每个pdf文件进行下载..下载文件后...我想将按钮名称“下载”更改为“查看”并更改相应的动作......还有一件事,我应该能够在重新打开应用程序后查看该文件,因为它已经下载了..
答案 0 :(得分:0)
对于进度条更新,请使用NSURLConnection委托,
示例:
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//here you will get total length
totalLength = [response expectedContentLength];
}
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// here calculate the downloaded length
[self.pdfData appendData:data];
//calculate downloaded percent
progress = ([self.pdfData length] * 100) / totalLength;
//and update the progress bar
}
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//save file here
//flip the corresponding button to view here
}
要查看下载的pdf文件,请检查文件是否已下载
示例:
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
// show the view button
}
else
//start download
答案 1 :(得分:0)
要使用NSURLconnection
计算下载进度,请使用以下委托功能。
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response
{
//In this function you will get the total size of PDF data you are receiving
fTotalSize=(float)[response expectedContentLength];
if(!datWebServiceData)
datWebServiceData =[[NSMutableData alloc]init];
//datWebServiceData = [[NSMutableData data] retain];
[datWebServiceData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data
{
//Here calculate the progress
float val=(((float) [datWebServiceData length] / (float) fTotalSize))*100;
lblProgress.text=[NSString stringWithFormat:@"%0.0f%%",val];
}
lblProgress.text将显示下载进度,datWebServiceData是NSData,它将包含PDF的全部数据。
现在要显示每个单元格的进度,请在NSMutableArray
中保存连接对象。然后检查委托的连接对象和数组中的所有连接异议,如果它们匹配则接受数组索引,然后相应地更新该单元的条目。
答案 2 :(得分:0)
首先创建UIButton的属性,如下所示 -
@property (strong,nonatomic) IBOutlet UIButton *btn;
然后进行连接。同样在viewDidLoad方法中执行如下操作 -
[_btn setTitle:@"DOWNLOADING" forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
因此,在您的startDownloadFile方法中添加以下内容 -
NSLog(@"PDF path: %@",filePath);
[data writeToFile:filePath atomically:YES];
/* ADD BELOW LINE */
[_btn setTitle:@"VIEW" forState:UIControlStateNormal];
然后对于buttonClicked:方法为 -
-(void)buttonClicked:(UIButton *)sender
{
if([sender.titleLabel.text isEqualToString:@"VIEW"])
{
/* Now as title is VIEW so call viewAction method */
[self viewAction:sender]
}
else
{
/*When tapped in DOWNLOADING mode nothing will happen */
}
}