Forum Cross Platform with Xamarin

Enable forward and back navigation in WKwebview

I am using WKwebview now for showing website or Google document .

But i read that when i want to put an app in the Appstore i have to do this

Enable forward and back navigation when appropriate in the Wkwebview

How do enable this ?

I am using Visualstudio 2019 and the app is for Android and Ios

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    WKWebView is a control for iOS.
    If you want to enable the forward and back navigation, try AllowsBackForwardNavigationGestures:

    WKWebViewConfiguration configuration = new WKWebViewConfiguration();
    WKWebView webView = new WKWebView(View.Bounds, configuration);
    View.AddSubview(webView);
    webView.AllowsBackForwardNavigationGestures = true;
    
  • BorisopritBorisoprit Member ✭✭✭

    Thanks for the reply LandLu ,

    I also found that site but don't know where to put the code.

    Have this public class in iOS MyWebViewRenderer

    Do i have to put it in here and when Yes where ?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Foundation;
    using UIKit;
    using Sponsor; 
    using Sponsor.iOS;
    using WebKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
    namespace Sponsor.iOS
    {
        public class MyWebViewRenderer : WkWebViewRenderer, IWKNavigationDelegate
        {
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                NavigationDelegate = this;
    
            }
    
            [Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
            public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
            {
                if (navigationAction.TargetFrame == null)
                {
                    webView.LoadRequest(navigationAction.Request);
    
                }
    
                decisionHandler.Invoke(WKNavigationActionPolicy.Allow);
            }
        }
    }
    
    
    
  • LandLuLandLu Member, Xamarin Team Xamurai
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
    
        AllowsBackForwardNavigationGestures = true;
        NavigationDelegate = this;
    }
    

    Put it here.

  • BorisopritBorisoprit Member ✭✭✭
    edited January 24

    I put it in there but there are no back or forward showing in the simulator in iOS , even when i google this site and go to that website for example.

    The Back in top is going back to the mainpage .

    And no navigation bar

  • BorisopritBorisoprit Member ✭✭✭

    Do i have to change something here ?

    The Navigating is for the ProgressBar

        <StackLayout Margin="20">
                <StackLayout Orientation="Horizontal">
    
    
    <Label x:Name="Welke"  Text=" " />
                </StackLayout>
                <!-- WebView needs to be given height and width request within layouts to render. -->
                <ProgressBar Progress="0.2"  
                        HorizontalOptions="FillAndExpand"  
                        x:Name="progress"  
                        ProgressColor="Maroon"    
                        IsVisible="True"/>
                <app1:MyWebView x:Name="webview" WidthRequest="1000" HeightRequest="1000"   Navigating="OnNavigating"  Navigated="OnNavigated"  />
            </StackLayout>
    
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    There won't be arrow buttons aside in the WKWebView. Try to test this on a real device. You will find it's easy to return to the previous page using left to right swipe gestures.

  • BorisopritBorisoprit Member ✭✭✭

    Thanks ,

    I tought something like that not showing or working on a simulator.

    Have account with App store recently en now i am trying to get it on Testflight .

    See how it looks.

Sign In or Register to comment.