# How to open and close a PDF file using Swift?

In this article we’ll see how to open a pdf file using swift in iOS. Here we’ll do it with an example of opening pdf in webView in iOS. Let’s create a project and add WKWebView to the storyboard.

Connect it’s outlet to the ViewController class.

Now we’ll see two different things

• Opening a PDF file from a URL on the web.

To open a web view from a url, first we need to have a url with a pdf file. In this example I’ll be using a dummy URL https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf

Let’s create a URL first,

let url: URL! = URL(string: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf")

Now the remaining steps are similar in both the methods, so let’s see method 2 and remaining steps.

• Opening a PDF from local storage.

if let pdfURL = Bundle.main.url(forResource: "dummy", withExtension: "pdf", subdirectory: nil, localization: nil) {
let request = URLRequest.init(url: pdfURL)
}