This is my code to download the pdf files using NSUrlConnection and i downloading them to the document directories.
-(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];
});
}
and am viewing the file using below code
-(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];
}
}
now i want to put the progress bar for each cell corresponding to each pdf file for downloading..and after downloading the file..i want to change the name of the button "download" to "view" and change the corresponding action..and one more thing i should able to view the file after re opening the application because it is already downloaded..