class ViewController: UIViewController { var webView: WKWebView!
override func loadView() { super.loadView()
let config = WKWebViewConfiguration() config.requiresUserActionForMediaPlayback = false
webView = WKWebView(frame: view.bounds, configuration: config) view.addSubview(webView) }
override func viewDidLoad() { super.viewDidLoad() let dirUrl = NSBundle.mainBundle().URLForResource("public_html", withExtension: "")! do { let dstUrl = try copyFilesToTempWWW(dirUrl) let url = dstUrl.URLByAppendingPathComponent("index.html") webView.loadRequest(NSURLRequest(URL: url)) } catch let error as NSError { print("Error: " + error.debugDescription) } }
func copyFilesToTempWWW(url: NSURL) throws -> NSURL { var error:NSError? = nil if (!url.fileURL || !url.checkResourceIsReachableAndReturnError(&error)) { throw error ?? NSError( domain: "BuggyWKWebViewDomain", code: 1001, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("URL must be a file URL.", comment:"")]) }
let fm = NSFileManager.defaultManager() let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory()).URLByAppendingPathComponent("www") try! fm.createDirectoryAtURL(tmpDirURL, withIntermediateDirectories: true, attributes: nil)
let dstURL = tmpDirURL.URLByAppendingPathComponent(url.lastPathComponent!) let _ = try? fm.removeItemAtURL(dstURL) try! fm.copyItemAtURL(url, toURL: dstURL)
return dstURL }
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
|