无法在 iPhone 中找到 PDF 文件

Cannot locate PDF file in iPhone

在我的一个 activity 中,我在屏幕上使用 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

那么 iPhone 中的 PDF 文件存储在哪里?

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

"../Library/354746396.pdf"

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

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

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

NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *savePath = [documents stringByAppendingPathComponent:@"filename.pdf"];
NSLog(@"savePath: %@", savePath);
- (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];
}

我的编码是正确的,但我找不到 pdf file.i 将解决我的问题 problem.i 将在设备中找到我的 pdf 文件。 有两种方法可以找到 pdf 文件。 1) 安装 iexplorer filemanager software.than 打开文件夹。 2) 将您的设备连接到 mac.select window 菜单,然后在 select 您的设备 name.select 您的应用程序之后单击 xcode.than 中的设备。

select显示container.than后就可以看到文件夹了....