Open PDF Tutorial

To display an PDF file from inside the app, it can be embedded inside a Web View. In this tutorial we will display a PDF file from the local filesystem onto the screen. This Tutorial is made with Xcode 7.1 and built for iOS 9.

Open Xcode and create a new Single View Application. For product name, use IOS9OpenPDFTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and add a Button to the Main View. Select the Button and change the title to "Open PDF". Ctrl and drag from the button to the top of the storyboard, hold down the Ctrl key and select the "Vertical Spacing to Top Layout Guide" and "Center Horizontally in Container" options.

The Storyboard will look like this.

Select the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl and drag from the Button to the ViewController class  and create the following Action

A sample pdf is needed so download the pdf and add it to the project. Go to the ViewController.swift file and implement the openPDF method.

@IBAction func openPDF(sender: AnyObject) {
    let url = NSBundle.mainBundle().URLForResource("Alice_In_Wonderland", withExtension: "pdf")
    if let url = url {
      let webView = UIWebView(frame: self.view.frame)
      let urlRequest = NSURLRequest(URL: url)

The location of the PDF file inside the application's bundle is assigned to the url constant. A web view is created containing the pdf and this web view is added to the main view. Build and Run the project, select the "Open PDF" button and the PDF will be displayed.

You can download the source code of the IOS9OpenPDFTutorial at the ioscreator repository on Github.