0

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

}

rmaddy
  • 314,917
  • 42
  • 532
  • 579
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • What have you researched / tried? – Wain Jan 27 '14 at 13:27
  • Edited question to answer this - I know it seems I have not looked in to it but when I research this it seems that what is suggested is a separate application to view it. That said I do not want to have to install a separate app. But it is possible to set a button to open a weblink making use Safari for example. Is there a similar method for a pdf viewer that is built into iOS or do you have to do it through your own views? If you can do both which is better? – RGriffiths Jan 27 '14 at 13:34

1 Answers1

2

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)

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thank you. I have added an update of where I have got up to. I think this is close but can't quite get there. Can you see what I am missing? – RGriffiths Jan 27 '14 at 14:26