Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Cancel Webview Navigation using "e.cancel = true" not working #4919

Closed
jgkent opened this issue Jan 8, 2019 · 18 comments
Closed

Cancel Webview Navigation using "e.cancel = true" not working #4919

jgkent opened this issue Jan 8, 2019 · 18 comments

Comments

@jgkent
Copy link

jgkent commented Jan 8, 2019

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
@kingces95
Copy link
Contributor

App74.zip

@kingces95
Copy link
Contributor

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!

@jgkent
Copy link
Author

jgkent commented Jan 10, 2019

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?

@candidodmv
Copy link

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

@candidodmv
Copy link

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

@jgkent
Copy link
Author

jgkent commented Jan 17, 2019

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.

@samhouts samhouts added the e/2 🕑 2 label Jan 24, 2019
@BrunoFreitasgit
Copy link

what is the timeframe for the fix release?

@jgkent
Copy link
Author

jgkent commented Feb 15, 2019

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.

@Johnnyrook777
Copy link

Johnnyrook777 commented Feb 21, 2019

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

@Ybs8770
Copy link

Ybs8770 commented Feb 25, 2019

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?

@xamarin xamarin deleted a comment from candidodmv Feb 25, 2019
@prashant0590
Copy link

@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?

@candidodmv
Copy link

@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

@candidodmv
Copy link

candidodmv commented Feb 26, 2019

@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.

@jgkent
Copy link
Author

jgkent commented Feb 27, 2019

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.

@Ybs8770
Copy link

Ybs8770 commented Feb 27, 2019

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?

@jgkent
Copy link
Author

jgkent commented Feb 27, 2019

@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.

@ExtremeKaos
Copy link

Same here, only in android Xamarin.Forms 3.4.0.1029999.

@heiser
Copy link

heiser commented Mar 4, 2019

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

@paymicro paymicro self-assigned this Mar 5, 2019
AxelUser pushed a commit to AxelUser/Xamarin.Forms that referenced this issue Jun 15, 2019
…n#4891 fixes xamarin#4919 closes xamarin#5460

* [Android] Fixes cancel Webview Navigation

* - added fix 4891
- refactoring

* Fix obsolete message

Co-authored-by: Ralph Brackert <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests