0

在我的一项活动中,我在 WebView 上生成了 pdf 文件并将此文件成功保存在文件夹中,但此文件路径显示为模拟器,然后轻松获取 pdf 文件,但是当我们在 iPhone 模拟器上运行我的应用程序时,文件成功显示在设备上并给出文件路径,但我找不到保存文件的位置如何获取此文件打开另一个问题,我正在使用此代码。

NSMutableData *data2 = [[NSMutableData alloc] initWithCapacity:byteArray.count];

for (NSNumber *byteVal in self.WebService->byteArray)
{
    Byte b = (Byte)(byteVal.intValue);

    [data2 appendBytes:&b length:1];
}

NSString *resourceToPath = [[NSString alloc]initWithString:[[[[NSBundle mainBundle]resourcePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"Documents"]];
fileName = [resourceToPath stringByAppendingPathComponent:@"myPDF.pdf"];

[data2 writeToFile:fileName atomically:YES];

NSURL *targetURL = [NSURL fileURLWithPath:fileName];
NSURLRequest *requestFile = [NSURLRequest requestWithURL:targetURL];

UIWebView *webView1=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];

webView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) ;
webView1.scalesPageToFit = YES;
webView1.scrollView.scrollEnabled =NO;

[webView1 loadRequest:requestFile];

[self.view addSubview:webView1];

下载pdf文件我正在使用这个.....

-(void)DownloadPdffile
{
    NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [docDirectories objectAtIndex:0];

    NSString *filePAth = [docDirectory stringByAppendingPathComponent:@"myPDF.pdf"];

    NSURL *url2 = [NSURL filePAth];

    UIDocumentInteractionController *docContr = [UIDocumentInteractionController
                                                 interactionControllerWithURL:url2];
    docContr.delegate=self;
    [docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}

使用此代码,我已成功将 Pdf 文件保存在模拟器中,但在 iPhone 设备中,我找不到我的 Pdf 文件在哪里,然后使用下载 pdf 然后.....打开此图像....!

在此处输入图像描述

但是当我点击任何一个选项时,我的应用程序就会关闭,所以告诉我如何在这些选项中打开 pdf 并保存在那里

4

1 回答 1

0

如果我理解您的要求正确,您需要在您的-(void)DownloadPdffile方法中以不同的方式引用您的文档文件夹。像这样:

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath [documentsDir stringByAppendingPathComponent:yourPdfFile.pdf];// your yourPdfFile file here
NSURL *url = [NSURL fileURLWithPath:pdfPath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];

docController.delegate = self;
[docController retain];
// docController is released when dismissed (autorelease in the delegate method)

BOOL isValid = [docController presentOpenInMenuFromRect:yourReadPdfButton.frame inView:self.view  animated:YES]; // Provide where u want to read pdf from yourReadPdfButton 

来自王子的回答

于 2013-07-30T06:03:32.907 回答