iosswiftswiftuiwebviewwkwebview

Customize allowsBackForwardNavigationGestures to only apply to back navigation, not forward


I have a simple webview in my SwiftUI iOS app. It works great but I wanted to add some navigation features so it's easier to look through the documentation page that opens up. Apple's Docs says I can use allowsBackForwardNavigationGestures to allow swipe gestures to go back or forward but I just need back not forward.

I've tried creating a DragGesture to override the default mechanism, but it didn't work properly. Should I just create my own gesture tracker and not use the property?

I'm fairly new to Swift and SwiftUI as well.


Solution

  • I was able to fix this issue by just removing forward navigation in the webView itself. Here's how in jQuery and Javascript:

    $(document).ready(function() {
      if (window.history && window.history.pushState) {
        window.history.pushState('', null, './'); // Replace './' with your desired URL
    
        $(window).on('popstate', function() {
          window.history.pushState('', null, './'); // Replace './' with your desired URL
        });
      }
    });