1

I'm displaying an OAtuh2 HTML page in WebView that returns me, after clicking on a validation button that is on this page, a redirect URL that I would like to intercept to use the information from it.

in XML file

<ContentPage.Content>
    <WebView x:Name="browser"></WebView>
</ContentPage.Content>

in CS file

browser.Source = "https://myUrl";

My low knowledge in Xamarin doesn't allow me to know how to do it

Thanks for your help

JBD
  • 568
  • 8
  • 25
  • 1
    See [WebView - Events](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=macos#events), and its `WebNavigatingEventArgs`. – ToolmakerSteve Nov 03 '21 at 21:39
  • @ToolmakerSteve on the WebNavigatingEventArgs witch "property" corresponding to the "redirect URL" ? Because e.Url return me the current URL and not the redirect one. – JBD Nov 05 '21 at 11:44
  • If you look at e.Source in debugger, it should contain either an `HTMLWebViewSource` or a `URLWebViewSource`. Which is it? And does it contain the original URL? If not, then in code behind for your WebView, what does `this.Source` contain? Should still be the original URL there, AFAIK. – ToolmakerSteve Nov 05 '21 at 20:11
  • It's URLWebViewSource that is triggered that contains only the original URL. And It triggers when the page is loaded, and not after the user clicked the "button validation", so when I will received the "redirect URL". So I tried WebNavigatedEventArgs to see if I can have later, but It's never trigger. – JBD Nov 07 '21 at 10:37
  • ok, I misunderstood. Sorry, I don't know why it does not trigger again when you click that button. The button takes you to another page, right? – ToolmakerSteve Nov 07 '21 at 22:50
  • 1
    I found out! I did both solutions, the one from ColeX and the one from you, to see how it works. When your doing this, your solution doesn't work. I've deleted ColeX solution and it works. Anyway Thank you so much for your patience with me, you're helped me a lot to progress. – JBD Nov 08 '21 at 09:34

1 Answers1

2

You can do as ToolmakerSteve mentioned using WebNavigatingEventArgs.

And for details you can implement this on each specific platform with custom renderer .

iOS

[assembly: ExportRenderer(typeof(WebView), typeof(MyRenderer))]
namespace FormsApp.iOS
{
    class MyRenderer : WkWebViewRenderer
    {

        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            this.NavigationDelegate = new MyDelegate();
        }
    }

    public class MyDelegate : WKNavigationDelegate
    {
        public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
        {
            if(navigationAction.NavigationType == WKNavigationType.Other)
            {
                if(navigationAction.Request.Url != null)
                {
                    //do something
                }
                decisionHandler(WKNavigationActionPolicy.Cancel);
                return;
            }
            decisionHandler(WKNavigationActionPolicy.Allow);
        }
    }
}

Android

[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyRenderer))]
namespace FormsApp.Droid
{
    class MyRenderer : WebViewRenderer
    {
        public MyRenderer(Context context):base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetWebViewClient(new MyClient());
            }
        }
    }

    public class MyClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
        {
            //do something
            return true;
        }
    }
}

Refer to

https://stackoverflow.com/a/45604360/8187800

https://stackoverflow.com/a/4066497/8187800

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • What proposed @ToolmakerSteve and your proposal are 2 different ways to get "redirect URL" ? – JBD Nov 05 '21 at 11:05
  • the problem now is how to get value from the xamarin.andorid or ios to xamarin.form ? – JBD Nov 05 '21 at 14:17
  • 1
    @JBD *"'how to get value from the xamarin.andorid or ios to xamarin.form"* - https://stackoverflow.com/q/24251020/199364, https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction – ToolmakerSteve Nov 08 '21 at 18:28
  • 1
    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center – ColeX Nov 09 '21 at 01:27