RGriffiths
RGriffiths

Reputation: 5970

Open a pdf saved in my documents folder in Xcode project

I have made an app that creates a pdf and stores it in the apps documents folder. I would now like to open it and view it from within the app when a 'View pdf' UIButton is pressed.

I have already looked at a few questions on here and I have considered either a separate view controller or perhaps a scroll view.

What is the best method to use?

UPDATE:

I have followed advice and I am trying to use QLPreviewController. I have added QuickLook framework and now have the following, but I am stuck on how to get the path recognised in the pathForResource. Any suggestions?

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}

- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
    return [NSURL fileURLWithPath:path];
}



- (IBAction)viewPdfButton:(id)sender {

NSString *filename= @"ObservationPDF.pdf";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
NSString *documnetDirectory = [path objectAtIndex:0];
NSString *pdfPathWithFileName = [documnetDirectory stringByAppendingPathComponent:filename];

[self generatePdf: pdfPathWithFileName];

QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentViewController:previewController animated:YES completion:nil];

}

Upvotes: 0

Views: 1432

Answers (1)

Wain
Wain

Reputation: 119031

If the PDF file is in the app documents folder then you shouldn't be thinking about passing it to another app, you should be looking to present the file inside the app. 2 general options:

  1. Add a UIWebView and load the local file into it
  2. Use QLPreviewController to show a new view containing the PDF

The web view is simple and requires no transition on the UI. The preview controller needs a transition but offers some sharing / printing support for free.


This line is confused (and invalid syntax by the looks of it):

NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];

You only use NSBundle to get items out of the bundle, and that isn't what you have. You should just be creating the URL with the file path where you save the file:

[NSURL fileURLWithPath:pdfPathWithFileName];

(which you may store or you may need to recreate in the same way as when you save the file)

Upvotes: 2

Related Questions