UIWebView Load PDF with Swift

Sometimes you just want to load a pdf in a UIWebView, so here’s an extension to help you. A little bit of encapsulation can make the world of difference in readability and maintainability.

extension UIWebView {
    func loadPDF(name:String!) {
        let termsPath:String? = NSBundle.mainBundle().pathForResource(name, ofType: "pdf")!
        let url = NSURL(fileURLWithPath: termsPath!)
        let pdfRequest = NSURLRequest(URL: url!)
        self.loadRequest(pdfRequest)
    }
}

Usage:

self.webView.loadPDF("Terms")

Leave a Reply

Your email address will not be published. Required fields are marked *