I can't wrap my head around the issue here: I save a PDF to disk - then load I try to load the PDF on a physical device and turn on airplane mode. File doesn't load. Can you help me ? It's clearly an issue of saving to and retrieving from the same directory.
SAVE LOCATION RETURNS:
Save location is : /var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Documents/fileurl.pdf
LOADING DIRECTORY RETURNS :
filePath is : /var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Documents/var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Documents/fileurl.pdf
SAVING THE PDF:
PDFViewController.m
- (IBAction)download:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Downloads"];
[request setPredicate:[NSPredicate predicateWithFormat:@"pubnumber = %@", self.title]];
[request setFetchLimit:1];
NSUInteger count = [context countForFetchRequest:request error:&error];
if (count == NSNotFound) {
} else if (count == 0) {
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:self.pdfURL]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:self.pdfURL];
[pdfData writeToFile:filePath atomically:YES];
NSLog(@"Save location is : %@", filePath);
NSManagedObject *newDWNLD = [NSEntityDescription insertNewObjectForEntityForName:@"Downloads" inManagedObjectContext:context];
[newDWNLD setValue:self.title forKey:@"pubnumber"];
[newDWNLD setValue:titleString forKey:@"pubtitle"];
[newDWNLD setValue:filePath forKey:@"puburl"];
if (![context save:&error]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Can't Save" message:@"Error handeling this request" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alertView.tag = 2;
[alertView show];
}
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Stand Fast!" message:@"This document is already in your Downloads library" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alertView.tag = 2;
[alertView show];
}
}
LOADING THE PDF:
DownloadsTableViewController.m
#import "PDFViewController.h"
@interface DownloadsTableViewController ()
@property (strong) NSMutableArray *downloads;
@property (weak) NSString *filePath;
@end
-(void)viewDidAppear:(BOOL)animated {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DWNLDView"];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Downloads"];
self.downloads = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
self.filePath = [self.downloads valueForKey:@"puburl"];
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DWNLDView"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PDFViewController *pdfVC = [storyboard instantiateViewControllerWithIdentifier:@"PDFViewController"];
pdfVC.title = cell.textLabel.text;
pdfVC.DWNLDSpassedURL = self.filePath;
pdfVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
pdfVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:pdfVC animated:YES completion:nil];
}
PDFViewController.h
#import "MBProgressHUD.h"
#import <CoreData/CoreData.h>
#import <Parse/Parse.h>
#import <MessageUI/MessageUI.h>
<UIWebViewDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate>
@property (weak, nonatomic) NSString *DWNLDSpassedURL;
PDFViewController.m
- (void)viewDidLoad {
if ([[DWNLDed objectForKey:@"DWNLDView"] isEqual:@YES]) {
[self LocalQuery];
}
}
- (void)LocalQuery {
NSLog(@"Local Query initiated");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:DWNLDSpassedURL];
NSLog(@"filePath is : %@", filePath);
NSURL *loadURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *localDOC = [NSURLRequest requestWithURL:loadURL];
[self.webView loadRequest:localDOC];
}
Check your filePath
in (void)LocalQuery
I think it's not true
DownloadsTableViewController.m
tableView:didSelectRowAtIndexPath:
set DWNLDSpassedURL = self.filePath
PDFViewController.m
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:DWNLDSpassedURL];
can't get true document filePath
//documentsDirectory : /var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Documents/
//DWNLDSpassedURL: /var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Documents/fileurl.pdf
try this link
EDIT: Solution:
PDFViewController.m
use NSString* filePath = DWNLDSpassedURL;
instead NSString* filePath = [documentsDirectory stringByAppendingPathComponent:DWNLDSpassedURL];
OR
DownloadsTableViewController.m tableView:(UITableView *)tableView didSelectRowAtIndexPath:
pdfVC.DWNLDSpassedURL = yourFileName;