Gralex
Gralex

Reputation: 4485

WKWebView history serialization on iOS

I'm trying to restore WKWebView navigation after app will restarts. I can inherit from base web view and take more control with it.

class WebViewHistory: WKBackForwardList {
// additional control with history items
// can perform custom init
}

class WebView: WKWebView {

    var history: WebViewHistory

    override var backForwardList: WebViewHistory {
        return history
    }

    init(frame: CGRect, configuration: WKWebViewConfiguration, history: WebViewHistory) {
        self.history = history
        super.init(frame: frame, configuration: configuration)
    }
}

For serialization I need serialize at least one WKBackForwardListItem. So here I face up with troubles.

class WebNavigationItem: WKBackForwardListItem {}

// but I cant create this objects
let item1 = WKBackForwardListItem() // 'init()' is unavailable
let item2 = WebNavigationItem() // 'WebNavigationItem' cannot be constructed because it has no accessible initializers

How to serialize history of WKWebView?


So for now I see only 1 option: make custom navigation with serialization support and forgot about allowsBackForwardNavigationGestures. And it required more code for implementing all logic of default web view history.

Upvotes: 1

Views: 621

Answers (1)

Pyry Jahkola
Pyry Jahkola

Reputation: 633

If you're targeting iOS 15 or above, you should look into WKWebView.interactionState.

It's erased to Any? but appears to be a plain old [NS]Data value behind the scenes. Maybe with luck you can serialise and restore that over app launches?

Upvotes: 2

Related Questions