I tried the new WebView api in swiftui and tried to pass webPage for this view to be able to control the navigation of the user by giving him the option to go back or forward using nav buttons but the view doesn't get's updated when the webPage.backForwardList.backList
so the buttons remains disabled.
code snippet:
@available(iOS 26, *)
struct LinkWebViewFor26: View {
let url: URL
@State var webPage = WebPage()
@Environment(\.dismiss) private var dismiss
var body: some View {
WebView(webPage)
.webViewBackForwardNavigationGestures(.disabled)
.task { webPage.load(url) }
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
dismiss()
} label: {
Image(systemName: "checkmark")
}
.buttonStyle(.glassProminent)
}
ToolbarItemGroup(placement: .topBarLeading) {
BackForwardMenu(
list: webPage.backForwardList.backList,
label: .init(text: "Backward", systemImage: "chevron.backward")
) { item in
webPage.load(item)
}
BackForwardMenu(
list: webPage.backForwardList.forwardList.reversed(),
label: .init(text: "Forward", systemImage: "chevron.forward")
) { item in
webPage.load(item)
}
}
}
.onChange(of: webPage.backForwardList) { _, _ in
print(webPage.backForwardList.backList)
}
}
}
Hi, thanks for trying out the new API! I’m able to reproduce the bug, and it seems related to how WebView interacts with SwiftUI’s observation system.
We will need to investigate this issue, as resolution may involve changes to Apple's software. I'd greatly appreciate it if you could open a bug report and post the FB number here once you do.
Bug Reporting: How and Why? has tips on creating your bug report.
In the meantime, one possible workaround is to observe changes to some other property on WebPage
, like
.onChange(of: webPage.isLoading) { }
for example, which will result in the back-forward list updating as expected.