Can't open PDF in NSCachesDirectory
puntofisso opened this issue · comments
I have posted this question on http://support.fastpdfkit.com/discussions/fastpdfkit-reader/629-open-pdf-stored-in-documents-folder but it looks like support has moved here.
Problem: If the pdf is in the bundle, I manage to open it with the standard "three minutes implementation". If I put the pdf in the NSCachesDirectory, it doesn't.
In other words, this code works:
NSString *documentName = @"193"; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *thumbnailsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",documentName]];
NSURL *documentUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:documentName ofType:@"pdf"]];
MFDocumentManager *documentManager = [[MFDocumentManager alloc]initWithFileUrl:documentUrl];
ReaderViewController *pdfViewController = [[ReaderViewController alloc]initWithDocumentManager:documentManager]; documentManager.resourceFolder = thumbnailsPath; pdfViewController.documentId = documentName; [self presentViewController:pdfViewController animated:YES completion:nil];
But the following code doesn't work - in spite of the variable "filename" containing the right path to the file in the NSCachesDirectory. The same file, put in the mainBundle with the previous code, works.
NSString* bookid = [NSString stringWithFormat:@"193"];
NSArray paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString thumbnailsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",bookid]];
NSString cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString filename = [NSString stringWithFormat:@"%@.pdf", bookid];
NSString *file = [cachesDirectory stringByAppendingPathComponent:filename];
NSString *cachePath = [paths objectAtIndex:0];
NSURL *documentUrl = [NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:file]];
MFDocumentManager *documentManager = [[MFDocumentManager alloc]initWithFileUrl:documentUrl]; ReaderViewController *pdfViewController = [[ReaderViewController alloc]initWithDocumentManager:documentManager]; documentManager.resourceFolder = thumbnailsPath; pdfViewController.documentId = documentName; [self presentViewController:pdfViewController animated:YES completion:nil];