question

Alfort-9796 avatar image
0 Votes"
Alfort-9796 asked LeonLu-MSFT edited

Xamarin Forms getting details of failed WebView navigation

I have a handler for the Navigated event of a WebView. Sometimes the navigation fails and the WebNavigatedEventArgs parameter of the handler has its property Result equals to WebNavigationResult.Failure.

Is there a way to get further information about the cause of the failure? Like a network access error, DNS error, a timeout, etc.?

Thank you

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

Hello,​

Is there a way to get further information about the cause of the failure? Like a network access error, DNS error, a timeout, etc.?

You can create a custom renderer for your WebView.
For Android, you can achieve WebViewClient class, get the further information about the cause of the failure from OnReceivedHttpError method.

public class CustomWebViewRenderer: WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            Control.SetWebViewClient(new MyWebViewClient());
        }
    }

    public class MyWebViewClient : Android.Webkit.WebViewClient
    {
        public override void OnReceivedHttpError(Android.Webkit.WebView view, Android.Webkit.IWebResourceRequest request, Android.Webkit.WebResourceResponse errorResponse)
        {
//get error from errorResponse
            base.OnReceivedHttpError(view, request, errorResponse);
        } 
    }

For iOS, you can achieve WKNavigationDelegate in webview's custom renderer and get error from DidFailProvisionalNavigation method.

public class CustomWebViewRenderer : WkWebViewRenderer
    {
       
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
           
            if (e.OldElement == null)
            {
                NavigationDelegate = new CustomWebViewDelegate();
            }
        }

    internal class CustomWebViewDelegate : WKNavigationDelegate { 
        public override void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigation, NSError error)
        {
       
            switch (error.Code)
            {
                case -1009: // no internet access
                    {
                     
                        break;
                    }
                case -1001: // timeout
                    {
                      
                        break;
                    }
                case -1003: // server cannot be found
                case -1100: // url not found on server
                default:
                    {
                        
                        break;
                    }
            }

        }
    }


Best Regards,

Leon Lu



If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

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.




· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Many thanks for your response.

I am using iOS and that seems to work but my Navigated event of the CustomWebView (previously WebView) is not working anymore. Is it because in the CustomWebViewDelegate I need to add all the common functionality that is available on the WebView? In other words, would I need to make a copy of the below original Xamarin class "CustomWebViewNavigationDelegate" and expand it with your suggestion?:

https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Renderers/WkWebViewRenderer.cs#L535-L641

I would not like to take that route as then I would be making an entire custom class and not benefiting of the official upgrades from Xamarin.

I guess inheriting from "CustomWebViewNavigationDelegate" is not an option. Would there be any other solution?

0 Votes 0 ·

. Would there be any other solution?

I afraid this answer is no, Xamarin.Forms do not provide Api to achieve it, you can make a feature request in the Xamarin GitHub page: https://github.com/xamarin/Xamarin.Forms/issues/new/choose
0 Votes 0 ·

Many thanks. The Xamarin Forms project is now closed for features requests but I have been able to put a request for .NET MAUI

https://github.com/dotnet/maui/issues/7159

0 Votes 0 ·
Show more comments