-1

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/Doc‌​uments/fileurl.pdf

LOADING DIRECTORY RETURNS :

filePath is : /var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Doc‌​uments/var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631‌​E/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];
}
Huy Nghia
  • 996
  • 8
  • 22
soulshined
  • 9,612
  • 5
  • 44
  • 79

1 Answers1

1

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/Doc‌​uments/

//DWNLDSpassedURL: /var/mobile/Containers/Data/Application/A1F1B294-9282-483B-B2E1-76C586A9631E/Doc‌​uments/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;
Community
  • 1
  • 1
Huy Nghia
  • 996
  • 8
  • 22
  • Hey Huy Nghia thanks for the reply. If you look at the very top of my question it states what the NSLog returns when i click save/download and then it states the return of the load path when I try to open the file. As you can see in view did load of DownloadsTableViewController i have self.filePath set after I get the coredata string. So its just returning an extra set of /var/mobile/Containers/Data/Application/etc – soulshined Nov 27 '14 at 08:17
  • 1
    I don't think so, try check NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSString *filePath = yourFilePath; BOOL success = [fileManager fileExistsAtPath:filePath]; – Huy Nghia Nov 27 '14 at 08:26
  • hmm good catch NSLog returned filePath doesn't exist. but its really weird how i'm able to pass the data. I also tried that link but can't figure it out. As you can tell i'm no expert in saving to disk and reading from disk. what would you recommend – soulshined Nov 27 '14 at 08:32
  • You are a blessing, it seems like i answered my own question earlier without knowing it when i said it sets an additional set of /var/mobile/Containers/Data/Application thats because I was appending it to that path again...you hit it on the dot. thanks – soulshined Nov 27 '14 at 08:49