Swift: Displaying Bundled PDF Documents With UIWebViews
One of the projects that I am working on includes a document library function. The function gives users the ability to view PDF documents (drawings, manuals, etc) that have been embedded into the app.
Displaying a bundled PDF document in a Swift-based iOS app is surprisingly easy. All that you need to do is load the PDF into a web view.
Here's a Swift code snippet that shows the technique.
// Get the document's file path.
let path = NSBundle.mainBundle().pathForResource("Newsletter.pdf", ofType: nil)
// Create an NSURL object based on the file path.
let url = NSURL.fileURLWithPath(path!)
// Create an NSURLRequest object.
let request = NSURLRequest(URL: url)
// Load the web viewer using the request object.
In the example, the bundled PDF document is titled "Newsletter.pdf," and "webView" is an IBOutlet to a UIWebView object.
That's really all there is to it. The PDF document will display nicely, and users will be able to scroll through the document, zoom in and out, and so on.
A screen shot that shows a PDF displayed in a web view is displayed below, and a Swift / Xcode project that demonstrates the technique is available here.