Saving WKWebView view state with interactionState.

I have a Swift iPhone app with tabbed pages. One of the pages is a WKWebView of a PDF document...

import SwiftUI
import WebKit


struct WebView: UIViewRepresentable {
    typealias UIViewType = WKWebView
    
    var webView: WKWebView
    
    init() {
        webView = WKWebView()
        webView.allowsBackForwardNavigationGestures = true
        webView.load(URLRequest(url: Bundle.main.url(forResource: "ByEye", withExtension: "pdf")!))
    }
    
    func makeUIView(context: Context) -> WKWebView { webView }
    
    func updateUIView(_ uiView: WKWebView, context: Context) { }  
}

struct ManualView: View {
    var webView = WebView()

    var body: some View {
        webView
    }
}


struct ManualView_Previews: PreviewProvider {
    static var previews: some View {
        ManualView()
    }
}

This works well for me, but it always goes back to the top of the document when the view is opened. This is a nuisance, as the user may be doing one of the exercises in the manual, and dabbling between the manual and the other pages.

The Apple documentation for interactionState suggests that this is the way to save the current view, but it does not say how. I cannot find any examples.

NB: My app only runs in portrait mode. This is a good thing for this view, as it means I am not closing the page in one mode and opening it in another.

I am using Xcode 14.1 building for iOS 16.0 on an iPhone 12 mini.

I have a partial fix for this. I found the ManualView was being recreated from scratch whenever the page was opened. I have re-arranged the code so the current ManualView is reselected by the TabView, with the scroll in the same place. I would like to see how interactionState works, but I don't need it now.

There is one odd, special case. If you follow a link in ManualView, and then swipe to go back, you still go back to page one, and there is a three second pause on my iPhone.

Saving WKWebView view state with interactionState.
 
 
Q