【问题标题】:Cannot locate PDF file in iPhone无法在 iPhone 中找到 PDF 文件
【发布时间】:2016-03-19 09:05:46
【问题描述】:

在我的一项活动中,我使用 WebView 在屏幕上显示了一个 PDF,并尝试使用此代码保存此 PDF:

_pageSize = CGSizeMake(width, height);

    NSString *newPDFName = [NSString stringWithFormat:@"%@", name];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];

    UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
     NSLog(@"path=%@",pdfPath);

当我在 iOS 模拟器(使用 Xcode)中运行此代码并显示路径并在文档文件夹中成功打开此 PDF 文件时,但当我在 iPhone 中运行此代码时,我得到了以下路径:

/var/mobile/Applications/0AF98361-C8DF-4C35-9E9F-EE48555185BC/Library/354746396.pdf

那么 PDF 文件存储在 iPhone 的什么位置?

【问题讨论】:

  • 请解决我的问题

标签: ios nsstring nsurl


【解决方案1】:

"..InDomains(NSLibraryDirectory, NSUserDomainMask, YES);"

"..//354746396.pdf"

您的路径在NSLibraryDirectory 中查找,它应该在NSDocumentDirectory 中查找,如下所示:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

如果仍然失败,请尝试以下代码:

NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *savePath = [documents stringByAppendingPathComponent:@"filename.pdf"];
NSLog(@"savePath: %@", savePath);

【讨论】:

  • 我会试试这个代码。我得到这个路径 /var/mobile/Applications/A42EA482-C921-4007-9DDF-D3181FE59177/Documents/354746393.pdf
  • 是的,这是正确的。它会起作用 :) 让我知道会发生什么。
  • yaa 但是如何在设备中找到这个 pdf 文件
  • 对不起,我的设备中找不到 pdf 文件
【解决方案2】:
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
    webView.scalesPageToFit = YES;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"];

    NSURL *targetURL = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webView loadRequest:request];

    [self.view addSubview:webView];
}

【讨论】:

  • 其他问题需要帮助
【解决方案3】:

我的编码是正确的,但我找不到 pdf 文件。我会解决我的问题。我会在设备中找到我的 pdf 文件。 有两种方法可以找到 pdf 文件。 1)安装 iexplorer 文件管理器软件。然后打开文件夹。 2)将您的设备连接到mac.select窗口菜单,然后在xcode中单击设备。然后选择您的设备名称。选择您的应用程序。

选择show container.than后就可以看到文件夹了....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-08
    • 2011-09-10
    • 1970-01-01
    • 2015-05-25
    • 2019-04-05
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多