Xamarin.forms: Cancel Webview Navigation using "e.cancel = true" not working

Created on 8 Jan 2019  路  18Comments  路  Source: xamarin/Xamarin.Forms

Have two separate Android apps running, both using a webview.

If the webview receives a URL which contains a "target=blank" parameter, this would open the link in the device's browser using Device.OpenUri(uri), and would cancel the navigation within the webview in the app. This was working fine using version 3.2.0 (have just rolled back and can confirm this still works), but after upgrading to version 3.4.0, in both apps, the link does open in the external browser, BUT also navigates within the app too. Therefore, it appears the "e.cancel = true;" seems to have ceased functioning here.

Steps to Reproduce

  1. Build a webview with code as follows:
//get the global webview set at app start
WebView WV = new WebView();

//create a webview and fill screen with it
WV.Source = url;
WV.HorizontalOptions = LayoutOptions.Fill;
WV.VerticalOptions = LayoutOptions.FillAndExpand;

//set the webview so that any links containing a querystring parameter of target=blank, then open these in the device's external browser, not in the webview
//    (NOTE - this NOT the target attribute of the <a> link, it needs to be in the querystring of the URL
WV.Navigating += (object sender, WebNavigatingEventArgs e) =>
    {
        if (e.Url.ToLower().Contains("target=blank") || e.Url.ToLower().Contains("target=_blank") || e.Url.ToLower().Contains("webapp/login"))
            {
                var uri = new Uri(e.Url);                            
                Device.OpenUri(uri);
                e.Cancel = true;
            }
                else
            {
                e.Cancel = false;
            };
        };
  1. Try two links (called from JS) from a remote website: eg -
<button onclick="window.location.assign('https://www.google.com/?target=blank'); return false;">Google Out</button>

<button onclick="window.location.assign('https://www.google.com/'); return false;">Google In</button>

Expected Behavior

The first link will open in the external browser and navigation stops in the app. The second link will open in the app.

Actual Behavior

in 3.2, it works fine. In 3.4, the first opens both in the external browser, but also in the app. Navigation is not cancelled.

Basic Information

  • Version with issue: 3.4 and 3.3
  • Last known good version: 3.2
  • IDE: Visual Studio 2017 v. 15.9.4
  • Platform Target Frameworks:

    • Android: 9.0

webview 2 Android bug

Most helpful comment

Have noticed the issue is actually still occurring for me. However, this issue appears twofold now:

1) If I use a "response.redirect" in C# with a "target=blank" in the url, then the issue occurs, (the e.Cancel=true is ignored).

2) If I use a javascript redirect in the rendered page (ie: window.location.href or window.location.assign), then I find that the e.Cancel does work - HOWEVER, I then find that the webview is then unresponsive. So whilst a user can be redirected to the external browser, when they return to the Webview in the app, it has ceased responding and needs to be closed on the device and reopened.

All 18 comments

image

Well, we're calling StopLoading() tho it looks like it was always a hack. It's being called inside OnPageStarted, which as the name implies is documented to fire after the new page is being loaded and well after, presumably, it's been fetched.

Anyway, we'll take a look as it's a regression and we are exposing an arg that implies we can stop the navigation!

Hi - thanks.

The issue I'm having seems is with a webview object created under the Xamarin.Forms.Webview class, and not the Android.Webkit.Webview - not sure if that makes a difference?

@jgkent me too have the same problem. The Webview not respect the "e.Cancel = true" and keeps going on to the next link.

the workaround that I'm using is downgrade to Xamarin.Forms v3.2 and now it's woking very well

My issue is solved.

I haven't changed any of my code, I rolled back to v3.2 last week which as we know works fine, and then 2 days ago I upgraded again to the latest version to test and it just worked.

Very strange.

what is the timeframe for the fix release?

Have noticed the issue is actually still occurring for me. However, this issue appears twofold now:

1) If I use a "response.redirect" in C# with a "target=blank" in the url, then the issue occurs, (the e.Cancel=true is ignored).

2) If I use a javascript redirect in the rendered page (ie: window.location.href or window.location.assign), then I find that the e.Cancel does work - HOWEVER, I then find that the webview is then unresponsive. So whilst a user can be redirected to the external browser, when they return to the Webview in the app, it has ceased responding and needs to be closed on the device and reopened.

Im seeing the same symptom #2 above.

Didn't matter if I changed the target to _blank or _self. Seems as soon as you cancel the nav, the webview becomes unresponsive

NOTE: Seems to affect Android, testing in iOS simulator was fine

Im seeing the same symptom #2 above.

Didn't matter if I changed the target to _blank or _self. Seems as soon as you cancel the nav, the webview becomes unresponsive

NOTE: Seems to affect Android, testing in iOS simulator was fine

I also encountered the same problem. Can you solve this problem now?

@jgkent Even I have a similar problem. My current Xamarin version is '3.4.0.1008975'. The Webview ignores the "e.Cancel = true" and keeps going on to the next link.

@candidodmv If I will downgrade it to '3.2.', will it affect anything else in my solution?

Is there any timeline on when this will be resolved?

@prashant0590 for me the only one thing that affected was the WebView.Refresh method that isn't available in this version 3.2, but i suggets to you review and compare both release notes for more details

@prashant0590 answering the second question: Is there any timeline on when this will be resolved?
how everything indicates, by someone(staff) that excludes my comment above this issue not appear urgent to them.

I got fed up with it.

For those that have the time or inclination, I spent 5 days ripping out the Xamarin.Forms.Webview, and have rebuilt the app using the Xamarin.Webkit.Webview instead. Syntax is different, but once you get a handle on it, it works well. Not experienced any of the weird behaviour as with the Forms.Webview.

I got fed up with it.

For those that have the time or inclination, I spent 5 days ripping out the Xamarin.Forms.Webview, and have rebuilt the app using the Xamarin.Webkit.Webview instead. Syntax is different, but once you get a handle on it, it works well. Not experienced any of the weird behaviour as with the Forms.Webview.

Hello, are you saying that you have solved this problem now? If so, can you provide your code?

@TopsKing I have not solved the issue, I am simply no longer using the Xamarin.Forms.Webview class. I am now using the Xamarin.Webkit.Webview class, as per: https://developer.xamarin.com/api/type/Android.Webkit.WebView/

The issue with the Xamarin.Forms.Webview class remains as far as the other users above report.

Same here, only in android Xamarin.Forms 3.4.0.1029999.

Same here. v3.5. A response from Xamarin would be nice.

Was this page helpful?
0 / 5 - 0 ratings