In this post we will learn how to load PDF in UIWebView.
Loading PDF in WebView is simple. Just follow the following steps.
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “PDFInWebView”
Step 2 − Open Main.storyboard and add UIWebView as shown below
Step 3 − Create @IBOutlet for the UIWebView, name it webview.
Step 4 − Add as sample PDF project. We will load this PDF in webivew. I am adding a PDF file named sample PDF.
Step 5 − Add the following lines in viewDidLoad method of ViewController
if let pdf = Bundle.main.url(forResource: "sample", withExtension: "pdf", subdirectory: nil, localization: nil) { let reqest = NSURLRequest(url: pdf) webView.loadRequest(reqest as URLRequest) }
In the above code we are first getting the URL to the sample pdf file, then loading that URL into the webview.
Step 6 − Run the project, you will see the PDF load in webview