I’ve had to display a pdf in just about every app that I’ve had to make. Whether it’s showing a menu, brochure, spec, flier, etc. Most of the requests have been to store the pdf in the app so that they can use it offline. So here’s how:

1. Fire up xcode, create a new “Single Window” project, give it a name, I called mine “pdfTest” and click next until you are past the initial creation.

2. Get a pdf and drag it into the pdfTest folder in the IDE and make sure to check the “Copy items into destination group’s folder”!!!



3. Click ViewController.h and add an outlet for a UIWebView, call it “pdfViewer”

#import
@interface ViewController : UIViewController{
IBOutlet UIWebView *pdfviewer;
}
@end

4. Click on “MainStoryboard.storyboard” in your IDE

5. Make sure you are zoomed in so that you can add items to your view.

6. Add a UIWebView to your Storyboard. It should fill up the board automatically

7. Click “View Controller” in the View Controller Scene to the left of your Storyboard, and click on the “Connections Inspector”, and drag from “pdfViewer” outlet to the UIWebView.

8. Click ViewController.m and add the following in your – (void)viewDidLoad

- (void)viewDidLoad{
// replace "test" with the name of your pdf. do not include the extension!
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
// this takes the path and turns it into a url our webview can load
NSURL *url = [NSURL fileURLWithPath:path];
// create the request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// load the request
[pdfviewer loadRequest:request];
// make it full size
pdfviewer.scalesPageToFit = YES;

[super viewDidLoad];
}

And that’s it. This might seem difficult if you are just beginning, but it’s really not. I promise.

2 thoughts on “Displaying a PDF in xcode 4.5.2 (iOS 6)

  1. This is perfect when I do project with one view controller. But When I try to open WebViewController from click of a tableview cell, its just giving blank view controller. I’m using xcode4.5

    Reply

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>