Xamarin Forms getting details of failed WebView navigation

Anonymous
2022-05-10T18:47:45.167+00:00

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

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

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-05-11T06:36:52.857+00:00

    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.


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.