Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从应用程序访问保存到iPhone的PDF?_Iphone_Pdf_Filesystems - Fatal编程技术网

如何从应用程序访问保存到iPhone的PDF?

如何从应用程序访问保存到iPhone的PDF?,iphone,pdf,filesystems,Iphone,Pdf,Filesystems,我创建了一个生成PDF并将其保存到NSDocumentDirectory的应用程序。我想让用户很容易找到文件,并用iPhone显示或打印。如何查找此文件并轻松查看它 以下是我用来创建PDF的代码: //USE PDFGENERATOR TO CREATE A PDF FILE PDFViewController* pdf = [[PDFViewController alloc] init]; NSString *header = lblTitle.text;

我创建了一个生成PDF并将其保存到NSDocumentDirectory的应用程序。我想让用户很容易找到文件,并用iPhone显示或打印。如何查找此文件并轻松查看它

以下是我用来创建PDF的代码:

       //USE PDFGENERATOR TO CREATE A PDF FILE
    PDFViewController* pdf = [[PDFViewController alloc] init];

    NSString *header = lblTitle.text;
    NSLog(@"fullQuote=%@", header);

    NSString *body = [NSString stringWithFormat:@"%@ \n\n - LRH \n %@", lblExcerpt.text, lblDesc2.text];
    NSLog(@"fullQuote=%@", body);

    pageSize = CGSizeMake(612, 792);
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", lblTitle.text];

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

    [pdf generatePdf :pdfFileName:header:body:nil];

有一个很好的教程演示了如何在应用程序中打开.pdf、.xls文件

您必须参考的主要类是
qlviewcontroller

这是您必须为此调用的数据源方法

- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index 
{
   // Break the path into it's components (filename and extension)
   NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

  // Use the filename (index 0) and the extension (index 1) to get path

  NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];

 return [NSURL fileURLWithPath:path];
}
-(id)previewController:(QLPreviewController*)控制器PreviewWitematindex:(NSInteger)索引
{
//将路径分解为其组件(文件名和扩展名)
NSArray*fileComponents=[[arrayOfDocuments objectAtIndex:index]由字符串@分隔的组件];
//使用文件名(索引0)和扩展名(索引1)获取路径
NSString*path=[[NSBundle mainBundle]pathForResource:[fileComponents objectAtIndex:0]类型:[fileComponents objectAtIndex:1]];
返回[NSURL fileURLWithPath:path];
}

还有人想问这个问题:

谢谢你,这很有帮助。我还想知道如何查找文件的简单说明,比如创建PDF的人现在想在我的应用程序外部查看该文件,或者以其他方式在文件系统上访问该文件。这样的事情可能吗?