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.