0

I'm trying to load an HTML page that is located in my local directory. It shows me this error "failed to load: data/Anctors.json" Also, this page works fine on Firefox but not working on Google Chrome. My code:

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
    let url = URL(string: "file:///Users/rashid/Documents/Developers/Swift/www/index.html")!
    webView.load(URLRequest(url: url))

    let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
    toolbarItems = [refresh]
    navigationController?.isToolbarHidden = false
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    title = webView.title
}

Screenshot

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rashid Latif
  • 2,809
  • 22
  • 26
  • 1
    Possible duplicate of [Load local html into UIWebView using swift](https://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift) – Scriptable Jan 21 '19 at 10:10

2 Answers2

0

You have to use Bundle.main.url(forResource: <#T##String?#>, withExtension: <#T##String?#>) method to load resources from within the app Refer the following link for more detailed answer (Possible Duplicate)

[1]: https://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift

Scriptable
  • 19,402
  • 5
  • 56
  • 72
Milan
  • 141
  • 4
0

Please add this code for allow content from document directory:

self.webView.loadFileURL( htmlfilePath, allowingReadAccessTo: documentDirUrl)

and make sure your html file and other content both are in same directory.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bhavesh Rathod
  • 178
  • 1
  • 1
  • 8