Reputation: 777
Okay, I'm burning my head a little here.
I am simply trying to save a PDF file that was loaded in a UIWebView.
This is what I have:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO)
{
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:@"MissaoPilotoPortuguese.pdf"];
NSData *pdfFile = [NSData dataWithContentsOfURL:webView.request.URL];
[pdfFile writeToFile:filePath atomically:YES];
But for some reason, when I call it to open the PDF file, the app crashes saying that the url path is nil:
NSString *path = [[NSBundle mainBundle] pathForResource:@"MissaoPilotoPortuguese" ofType:@"pdf"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
Any ideas on what I'm doing wrong?
Thanks
Upvotes: 1
Views: 940
Reputation: 52347
In your code to save, you construct the URL by asking for the cache directory, and then appending the file name.
Then, when you try to open it in the second snippet, you simply ask for the directory of the main bundle -- this isn't the same directory as you have above.
Your second snippet should probably look more like:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *filePath = [cachePath stringByAppendingPathComponent:@"MissaoPilotoPortuguese.pdf"];
NSURL *urls = [NSURL fileURLWithPath:filePath];
...
Or, if it's all happening within the same method, just reuse filePath
from when you had constructed it before.
Upvotes: 3