【问题标题】:Save locally generated PDF to app's documents folder in iOS8在iOS8中将本地生成的PDF保存到应用程序的文档文件夹中
【发布时间】:2015-08-08 08:20:41
【问题描述】:

执行此操作时,我的应用似乎不再能够将文件保存到其文档文件夹:

        NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];

        NSData *dta = [[NSData alloc] init];
        dta = [NSData dataWithContentsOfFile:path];

        NSLog(@"writing file: %@", path);


        if ([dta writeToFile:path options:NSAtomicWrite error:nil] == NO) {
            NSLog(@"writeToFile error");

        }else {

            NSLog(@"Written: %@", path);
            [self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
        }

我确实在日志中得到了完整路径的“Written”: 撰写:/var/mobile/Containers/Data/Application/ECBCD65D-A990-4758-A07F-ECE48E269278/Documents/9d440408-​​4758-4219-9c9c-12fe69cf82f2_pdf.pdf

只要我不退出应用程序,我就可以像这样在 UIWebView 中加载 PDF(pdfPath 是我传递给加载 PDF 文件的函数的字符串):

[www loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pdfPath]]];

一如既往地非常感谢任何帮助。

【问题讨论】:

  • 当我查看日志时,似乎“/Application/”之后和“/Documents/”之前的地址不断变化。来自“/Documents/”的子字符串会起作用吗?好像有点丑……

标签: pdf ios8 uiwebview nsdocumentdirectory writetofile


【解决方案1】:

好吧,经过一番讨论后,我发现由于每次运行应用程序时文档目录都会更改路径,因此我不得不:

  1. 将文件写入文档文件夹,并附上完整路径 文件:

    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES ); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent: fileName];

    NSData *dta = [[NSData alloc] init];
    dta = [NSData dataWithContentsOfFile:path];
    
    if ([dta writeToFile:path options:NSAtomicWrite error:nil] == NO) {
        NSLog(@"writeToFile error");
    
    }else {
    
        NSLog(@"Written: %@", path);
        [self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
    }
    
  2. 仅将文件名保存到我的数据库中

  3. 使用新生成的完整 Documents 目录路径加载文件,并从我的数据库中附加文件名:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent: pdfPath];

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2019-05-15
    • 2017-02-01
    • 1970-01-01
    相关资源
    最近更新 更多