How to close(PopAsync) a WebView from a link click?

Hi i'm currently opening a WebView with xamarin forms and use a redirect URL to display a 3rd Party login page.

When you user either create a new account or logs in to the 3rd party, the 3rd party then calls a callback URL which we have specified in the backend.

Once the callback is returned i want to show the user some information and the but a link on the HTML page.

When the link is pressed i want to close the webview to return back to the Xamarin Forms App.

Can this be done?

Thanks

Answers

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Try something like this.

    myWebView.Navigating += async (sender, args) => {
        args.Cancel = true; // prevents the webview navigation from handling
        await Navigation.PopAsync();
    };
    
  • TroyScarbroughTroyScarbrough USMember

    I am new to programming and helping to work on a phone app. I see statements similar to what you just stated. That is obviously on the cs file. How would the above look on the xamarin forms file? Or is it only going to be seen on the cs file?

  • ChaseFlorellChaseFlorell CAInsider, University mod

    This is in the codebehind file attached to the xaml file

  • CostasAletrariCostasAletrari USMember ✭✭

    Hi @ChaseFlorell Thank you for your reply, after adding the code to the codebehind the navigating event does not seem to trigger when the button is pressed

     var browser = new WebView
            {
                Source = url,
    
    
            };
            Content = browser;
    
            browser.Navigating += async (s, e) =>
            {
                if (e.Url.StartsWith("https://www.google.com"))
                {
                    e.Cancel = true;
                    await Navigation.PopAsync();
    
                }
    
            };
    

    and the html with the button looks like this

    Return To App

    I've tried debugging to see if it get hit but to no success

    the code above is also in the constructor of the webview page

    any ideas?

Sign In or Register to comment.