Search code examples
swiftwebkitwkwebview

Swift/JS Restore WebView Back Forward List


I am trying to figure out how to restore the back forward list in a WKWebView. Apple defaults the BackForwardList to be readonly. However, FireFox, Chrome, and other mobile browsers are able to restore it each session.

This is how FireFox restores it on iOS: https://github.com/mozilla-mobile/firefox-ios/wiki/History-Restoration-in-WKWebView-(and-Error-Pages)

Here is the JS/HTML code that's used to access the DOM History API: https://github.com/mozilla-mobile/firefox-ios/blob/041c85483b1aa4a4e357d8bd9419550e4f20347a/Client/Assets/SessionRestore.html

In order to load this file and restore a WKWebView's history, do you need to spin up a local server within the app?

It seems like you would need to in order to access a local:// url that points to this JS file.


Solution

  • Yes, that's what they are doing, according to WebServer.swift. There are many references in the code to internal://, and an InternalURL struct in URLExtensions.swift. There are session restoration things in that file as well, such as var isSessionRestore: Bool.

    If you haven't already, clone the project and open it in Xcode for easier navigation and searching.