Links inside of a Xamarin.Forms/Xamarin.Android WebView won't work

Raghavendra G R 121 Reputation points
2021-06-23T13:18:45.373+00:00

Hi,

I'm loading an webpage on a webview in my xamarin forms app, earlier(android 10 and before) i was able to get callbacks 'ShouldOverrideUrlLoading' when i click on an link(href) inside my already loaded webpage.
But in recent days (Android 11) i'm not getting a callback on clicking on any links. However 'ShouldOverrideUrlLoading' will be called for the 1st time but subsequent click on links i'm not getting.

Please let me know if anybody faced this earlier and found a solutions by now..!

Thanks..

Note: Same works in iOS.

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-06-24T03:09:45.553+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I test it in my Android 11 environment, and set the target-framework to the android 11.

    I test WebView source is <WebView Source="https://www.google.com.hk/" WidthRequest="200" HeightRequest="500"></WebView>. And here is my webview custom renderer.

       [assembly: ExportRenderer(typeof(WebView), typeof(MyWebViewRenderer))]  
       namespace WebViewDemo.Droid  
       {  
           class MyWebViewRenderer : WebViewRenderer  
           {  
               public MyWebViewRenderer(Context context) : base(context)  
               {  
               }  
         
               protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)  
               {  
                   base.OnElementChanged(e);  
         
                   Control.Settings.JavaScriptEnabled = true;  
         
                   Control.SetWebViewClient(new MyWebViewClient());  
               }  
           }  
         
           internal class MyWebViewClient : Android.Webkit.WebViewClient  
           {  
               public override Android.Webkit.WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, Android.Webkit.IWebResourceRequest request)  
               {  
                   return base.ShouldInterceptRequest(view, request);  
               }  
               public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, Android.Webkit.IWebResourceRequest request)  
               {  
                   return base.ShouldOverrideUrlLoading(view, request);  
               }  
           }  
    

    I add the breakpoint in the ShouldOverrideUrlLoading method. when I click the links, ShouldOverrideUrlLoading will be executed as excepted like following screenshot.

    108851-image.png

    My Xamarin forms version is 5.0.0.2012, VS version is 16.10.0. You can create a new project to make a test.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.