Skip to content

Instantly share code, notes, and snippets.

@nacho4d
Created February 23, 2015 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nacho4d/e78554dcaa8ae6b2c4c7 to your computer and use it in GitHub Desktop.
Save nacho4d/e78554dcaa8ae6b2c4c7 to your computer and use it in GitHub Desktop.
// There is a bug in WebKit
// Local files cannot be read unless they are in the tmp/www/ directory!
// Example: read "file.pdf" into WKWebView
var fileManager = NSFileManager.defaultManager()
var srcPath = NSBundle.mainBundle().pathForResource("file", ofType: "pdf")
var tmpPath = NSTemporaryDirectory().stringByAppendingPathComponent("www")
// TODO: handler errors correctly
fileManager.createDirectoryAtPath(tmpPath, withIntermediateDirectories: true, attributes: nil, error: nil)
var dstPath = tmpPath.stringByAppendingPathComponent("file.pdf")
if !fileManager.fileExistsAtPath(dstPath) {
// TODO: handle errors correctly
fileManager.copyItemAtPath(srcPath!, toPath: dstPath, error: nil)
}
// End of
let request = NSURLRequest(URL: NSURL.fileURLWithPath(dstPath)!)
self.webView.loadRequest(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment