1

I have an app that uses at WKWebView to display local file paths. It segues to another view controller, then unwinds to the original controller, with a new html file path. I have got it loading the new file path but only after the index file flashes for a second. Anyway to clear this view?

override func viewWillAppear(_ animated: Bool) {

if unwindingFromPost == true {

      let htmlPath = Bundle.main.path(forResource: "card/card", ofType: "html", inDirectory: "pickles_ui")

      let url = URL(fileURLWithPath: htmlPath!)
      let request = URLRequest(url: url)

      webView.scrollView.bounces = false

      webView.load(request)

    } else {
        let htmlPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "pickles_ui")

        let url = URL(fileURLWithPath: htmlPath!)
        let request = URLRequest(url: url)

        webView.scrollView.bounces = false

        webView.load(request)
  }
}

}

1 Answers1

2

You might have this behaviour because it might show the previous url in webview just like a second before loading the new one. I can suggest a workaround like, loading a blank view on your webView when view disappears (viewWillDisAppear method):

In your main viewController try adding following:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    let url = URL(string: "about:blank")
    let request = URLRequest(url: url!)
    myGraphView.loadRequest(request)
}
emrepun
  • 2,496
  • 2
  • 15
  • 33
  • 1
    I'm happy to hear. Also really liked your username considering the way some old SO users grills new contributors :D By the way can you select my answer as accepted? – emrepun Jan 22 '19 at 23:16
  • 1
    Thats why I chose it. :) Thank you again, it was holding me up all day. – pls_dont_yell Jan 22 '19 at 23:27
  • I was having issues while loading webview in tabbar, tabbs when switched were showing old loaded data, then I followed above method and now it works fine. Thank you – vilas deshmukh Mar 24 '21 at 10:56