I have received several requests asking how to view a PDF after creating it natively in the iPhone. Below is a guide to doing just that. It uses the method I am currently using - a UIWebView. This provides you scrolling, zooming and navigating multiple pages without having to write any extra code. So, on to the code!
- In your project navigate to or create the directory you want to have the PDF Viewer.
- Create a new UIView subclass named 'PDFViewController'.
- Also create a new view in your resources directory named 'PDFView'.
This is what your PDFViewController.h should look like:
Code:
@interface PDFViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
NSURL *pdfUrl;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSURL *pdfUrl;
@end
Here we have created UIWebView object named webView, (which has an outlet to Interface Builder) and an NSURL object named pdfUrl.
This is what your PDFViewController.m should look like:
Code:
#import "PDFViewController.h"
@implementation PDFViewController
@synthesize webView, pdfUrl;
#pragma mark -
#pragma mark UIViewController methods
// View Did Load method -- Load the PDF
- (void)viewDidLoad {
[super viewDidLoad];
// Tells the webView to load pdfUrl
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
}
// Dealloc method -- webView, pdfURL
- (void)dealloc {
[webView release];
[pdfUrl release];
[super dealloc];
}
@end
We synthesize our properties, release them in our deallocate method, and in our ViewDidLoad method, tell the webView to load pdfUrl.
Next open PDFView in Interface Builder.
- Make File's Owner Class Identity 'PDFViewController'.
- Add a Web View on top of the View.
- Hook up the view outlet on File's Owner to the View, and the webView outlet on File's Owner to the WebView.
- Hook up the delegate outlet on Web View to File's Owner.
- Save the file and quit Interface Builder.
Now the only other thing we need to do is Create an instance of PDFViewController, set the pdfUrl property and push the view. I am assuming here that you have some basic knowledge and am not going to get too detailed as to where you put the code that follows. Make sure that in the controller you place this code you have imported 'PDFViewController.h', otherwise it won't work.
Code:
// Create an instance of PDFViewController
PDFViewController *controller = [[PDFViewController alloc] initWithNibName:@"PDFView" bundle:nil];
// Get the path to our documents directory
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// This should be our documents directory
NSString *saveDirectory = [documentPath objectAtIndex:0];
// Our PDF is named 'Example.pdf'
NSString *saveFileName = @"Example.pdf";
// Create the full path using our saveDirectory and saveFileName
NSString *finalPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
// Set the pdfUrl to our finalPath
controller.pdfUrl = [NSURL fileURLWithPath:finalPath];
// Push 'controller'
[self.navigationController pushViewController:controller animated:YES];
// Release 'controller'
[controller release];
So, above we created an instance of PDFViewController, created a string that contains the path to our documents directory and a file named 'Example.pdf' in that directory. We set that string to pdfUrl for our controller, and pushed our controller.
That about does it, viewing PDFs on the phone couldn't be much easier than that! Please let me know if you have any questions or if something presented in this tutorial doesn't click.
Thanks!