【问题标题】:Objective C - Cannot read file in Directory of Document or Cache in Iphone目标 C - 无法读取 Iphone 文档目录或缓存中的文件
【发布时间】:2011-09-30 12:51:27
【问题描述】:

在我的工作中,我只想在 iphone 设备中阅读下载的 PDF

起初,我尝试使用“bundleRoot”目录,它可以在模拟器上工作,但不能在设备上工作。现在,我只是在模拟器中测试 Document of Document 或 Cache。

How can I get a writable path on the iPhone?

看完上面的题目,

我尝试过使用文档目录或缓存目录。

这是我查找可写路径的代码

/**************************************************** ************************************/

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;

if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}


NSString *path_to_file = [cachePath stringByAppendingPathComponent:@"testing6.pdf"];

/**************************************************** ************************************/

我可以在这个目录中找到下载的文件并获取大小,但是我无法使用以下代码读取文件:

            pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

    page = CGPDFDocumentGetPage(pdf, 1);
    CGPDFPageRetain(page);

    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

返回pageRect.size.width为0

我打印出下载和检索文件路径后也是:

/Users/ITDEV/Library/Application Support/iPhone Simulator/4.3.2/Applications/806ED359-70F8-4C07-996F-6AFC959B3FC7/Library/Caches/testing6.pdf

谁能帮帮我?

【问题讨论】:

    标签: iphone objective-c caching document


    【解决方案1】:

    如果所有其他答案都不起作用,请尝试使用 NSBundle,如下所示:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"myfilename" ofType:@"pdf"];
    NSLog("path : %@",path);
    

    我遇到了同样的问题,我发现我的 plist 文件不在 Documents 目录中,而是显示了我的应用程序的路径,然后显示了 MYAppName.app/myPlist.plist 文件。这意味着应用程序文件将我的 plist 文件包含在它自己的包中,即 MainBundle。

    【讨论】:

    • 感谢您的回复。我以前也尝试过这种方式(使用捆绑包)。它可以在模拟器中完美运行,但不能在设备上运行。有人说 Bundle 在设备中不可写。这是真的吗?
    • 我相信当文件捆绑在一起时,它们将不可写。但我不确定。
    • 所以我无法将 pdf 文件保存到设备的捆绑包中 T_T
    • 不,您不能保存在 MainBundle 中。但是您可以保存在 Document's Directory 中,这是应该放置资源的实际位置。
    • 我正在使用文档目录,但我无法以编程方式检索下载的文件,即使将其加载到 webview 中,我也无法做到这一点。这个文件夹有权限设置吗?
    【解决方案2】:

    您可以在 UIWebView 中读取文件。

    您可以使用以下代码打开文件。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    BOOL isDir = NO;
    NSError *error;
    
    if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
    }
    
    
    NSString *path_to_file = [cachePath stringByAppendingPathComponent:@"testing6.pdf"];
    
    NSURL *targetURL = [NSURL fileURLWithPath:path_to_file];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [YourWebViewName loadRequest:request];
    

    别忘了将你的 webview 的 IBOutlet 与 nib 文件的 UIWebview 连接起来。

    【讨论】:

    • 感谢您的回复。我以前用这种方式在我的应用程序中查看 PDF,但我们认为这还不够好。我们希望在查看之前将小尺寸的 PDF 存储到应用程序中。
    • 你有那个 pdf 文件的 url,你想从哪里存储到你的应用程序路径中。
    • 是的!我可以完美地从 url 下载 PDF 到 Iphone 文档目录!但是我无法以编程方式打开它,如果我直接进入该文件夹,我可以手动打开 pdf。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2011-10-09
    相关资源
    最近更新 更多