0

I have some static HTML pages, I load into a WKWebView by using the loadHTMLString(_:baseURL:) method. A href link inside a local HTML page loads another local HTML page with the same method.

The history navigation between the loaded local HTML pages have to work with swipe gestures (back and forward), so I set: webView.allowsBackForwardNavigationGestures = true

localHTML <-(swipe back/forward)-> localHTML <-(swipe back/forward)-> localHTML

The goal is to have exactly the same optical behavior like the swipe navigation when using load(_:URLRequest), that means the preview of the upcoming history item (back and forward item).

(There was a similar question by another user, but the solution doesn't work for my problem: iPhone UIWebView loadHtmlString not added to history)

SchmidtFx
  • 498
  • 4
  • 16

1 Answers1

0

WKWebView navigation gestures (swipe back and forward through history) works fine with load(_:URLRequest). It seems there is no solution with loadHTMLString()

A possible solution for the problem is to load the local HTML pages by using init(fileURLWithPath:) of type URL.

override func viewDidLoad() {
    super.viewDidLoad()

    // WKWebView configuration ...

    webView.allowsBackForwardNavigationGestures = true

    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "page1", ofType: ".html")!)
    let request = URLRequest(url: url)
    webView.load(request)
}

func loadPage2() {
    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "page2", ofType: ".html")!)
    let request = URLRequest(url: url)
    webView.load(request)
}

After loading local HTML pages with fileURLWithPath, the swipe navigation gestures works fine.

SchmidtFx
  • 498
  • 4
  • 16