【问题标题】:Saved PDF to Disk; won't Load in WebView Offline将 PDF 保存到磁盘;不会在 WebView Offline 中加载
【发布时间】:2014-11-27 07:27:56
【问题描述】:

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

保存位置返回:

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

加载目录返回:

文件路径为:/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

保存 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:

下载TableViewController.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];
}

【问题讨论】:

  • 加载 PDF 会有些延迟
  • @NikitaKhandelwal 没用

标签: ios pdf nsfilemanager


【解决方案1】:

(void)LocalQuery 中检查你的filePath 我认为这不是真的

下载TableViewController.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];

下载TableViewController.m tableView:(UITableView *)tableView didSelectRowAtIndexPath:

pdfVC.DWNLDSpassedURL = yourFileName;

【讨论】:

  • Hey Huy Nghia 感谢您的回复。如果您查看我的问题的最顶部,它会说明当我单击保存/下载时 NSLog 返回的内容,然后当我尝试打开文件时它会说明加载路径的返回。正如您在视图中看到的那样,在获取 coredata 字符串后,我设置了 self.filePath 的 DownloadsTableViewController 加载。所以它只是返回一组额外的 /var/mobile/Containers/Data/Application/etc
  • 我不这么认为,尝试检查 NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *错误; NSString *filePath = yourFilePath; BOOL 成功 = [fileManager fileExistsAtPath:filePath];
  • hmm 很好的捕获 NSLog 返回的文件路径不存在。但它真的很奇怪我如何能够传递数据。我也尝试了该链接,但无法弄清楚。如您所知,我不是保存到磁盘和从磁盘读取的专家。你会推荐什么
  • 你真是太幸运了,当我说它设置了一组额外的 /var/mobile/Containers/Data/Application 那是因为我在附加它时,似乎我早先回答了我自己的问题而不知道它再次走上那条路……你一针见血。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-12-25
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2020-12-17
  • 1970-01-01
相关资源
最近更新 更多