2012-03-01 69 views
1

我創建了一個生成PDF並將其保存到NSDocumentDirectory的應用程序。我想讓用戶很容易找到文件並顯示或使用iPhone打印。我如何找到該文件並輕鬆查看?如何從應用程序訪問保存到iPhone的PDF?

這裏是我用來創建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]; 

回答

0

有一個可用here一個很好的教程演示如何打開的.pdf,.xls的應用程序中的文件。

您必須參考的主要類是QLPreviewControllerhere

這是數據源的方法,你就必須調用該

- (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]; 
} 

也有人想提及這太問題:iPhone - Opening word and excel file without using UIWebview

+0

謝謝你,這是非常有幫助的。我還希望簡單指導如何查找文件,例如創建PDF的人員,現在想要在應用程序的外部查看它,或者以某種其他方式在文件系統上訪問文件。是甚至可能的嗎? – jroyce 2012-03-01 05:55:29

相關問題