Load edited local file in WKWebView

Hi,


I'm trying to load a local html file where I just saved some changes to in a WKWebView. I used to do it with loadHTMLString(), but unfortunatly there seems to be no way to enable back/forward swipe gestures using loadHTMLString().


Writing to the file works just fine, but I can't seem to load the edited file. I keep getting the original. Anyone know what I'm doing wrong here?




let file = "localFile.html"

        let text = "Some changes"

        if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

            let fileURL = dir.appendingPathComponent(file)

            //writing
            do {
                try text.write(to: fileURL, atomically: false, encoding: .utf8)
            }
            catch {/* error handling here */}

            //reading
            do {
                let loadedText = try String(contentsOf: fileURL, encoding: .utf8)
                print(loadedText) // checking saved text
                
                let url = URL(fileURLWithPath: Bundle.main.path(forResource: "localFile", ofType: ".html")!)
                let request = URLRequest(url: url)
                webView.load(request)

            }
            catch {/* error handling here */}
        }

Replies

Have you tried adding a swipe gesture recognizer?

You should be able to use that to simulate the forward/backward swipe.


Have you tried triggering a reload after updating the file?

Thanks for the reply Jilest002.


I added the following below and the navigation gestures work fine when I click a link.

webView.allowsBackForwardNavigationGestures = true


I just need to solve the problem that I can't seem to display the edited content in the stored file. What is the best way to trigger a reload after writing to the file?

There is a reload method on the wkwebview, which might be a worst case option.

It strikes me that you have a caching issue though.

I don't see a really good option in the

WKWebsiteDataStore.

The NSURLRequest method has some caching options, but I'm not positive it works with file url's. I haven't done much with local files in a while.