将PDF保存到磁盘;不会在WebView Offline中加载

时间:2014-11-27 07:27:56

标签: ios pdf nsfilemanager

我无法解决这个问题:我将PDF保存到磁盘 - 然后加载我尝试在物理设备上加载PDF并打开飞行模式。文件没有加载。你能帮助我吗 ?这显然是保存到同一目录并从中检索的问题。

保存位置退货:

保存位置为:/ var / mobile / Containers / Data / Application / A1F1B294-9282-483B-B2E1-76C586A9631E / Doc uments / fileurl.pdf

加载目录返回:

filePath是:/ var / mobile / Containers / Data / Application / A1F1B294-9282-483B-B2E1-76C586A9631E / Doc uments / var / mobile / Containers / Data / Application / A1F1B294-9282-483B-B2E1-76C586A9631 E /文档/ fileurl.pdf

保存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];
}
}

加载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];
}

1 个答案:

答案 0 :(得分:1)

检查filePath (void)LocalQuery我认为不正确

DownloadsTableViewController.m

tableView:didSelectRowAtIndexPath:设置DWNLDSpassedURL = self.filePath

PDFViewController.m

NSString* filePath = [documentsDirectory stringByAppendingPathComponent:DWNLDSpassedURL];无法获得真实的文档文件路径

//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

尝试this link

修改 解决方案:

PDFViewController.m

使用NSString* filePath = DWNLDSpassedURL;代替NSString* filePath = [documentsDirectory stringByAppendingPathComponent:DWNLDSpassedURL];

DownloadsTableViewController.m tableView:(UITableView *)tableView didSelectRowAtIndexPath:

pdfVC.DWNLDSpassedURL = yourFileName;