graph User
graph User

Reputation: 45

Maui 6 WebView Timout handling

Im trying to handle webview timeout in my project.

If i want to check for network connectivity at startup , i can do this :

if(!CrossConnectivity.Current.IsConnected)
{
   //You are offline, notify the user
   return null;
}

but the problem is , i want to check exact website for connectivity.

and this should happen during navigation inside website.(may be some pages goes timeout and i should

show the message to person)this is my code :

string url = "xxxxxxxxx.xxxxxx";
webview.Source = url;
Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("Custom", (handler, view) =>
{
#if ANDROID
    handler.PlatformView.Settings.CacheMode = Android.Webkit.CacheModes.Default;
    handler.PlatformView.Settings.SetAppCacheEnabled(true);
    handler.PlatformView.Settings.JavaScriptEnabled = true;
    handler.PlatformView.AddJavascriptInterface(new JavaScriptHandler(), "Native");
#endif
});

Ive tried the Maui IsConnected method but it doesn't solve my problem.

Upvotes: 0

Views: 417

Answers (1)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 8220

WebView has Navigation and Navigated events

You may try using Navigated event for webview.

<WebView x:Name="mywebview" Navigated="mywebview_Navigated" .../> 

And in code-behind event handler, you could implement your logic (such as display an alert) based on the result like this:

void mywebview_Navigated(System.Object sender, Microsoft.Maui.Controls.WebNavigatedEventArgs e)
{
    switch (e.Result)
    {
        case WebNavigationResult.Cancel:
            // your logic
            break;
        case WebNavigationResult.Failure:
            // your logic
            break;
        case WebNavigationResult.Success:
            // your logic
            break;
        case WebNavigationResult.Timeout:
            // your logic
            break;
        default:
            // your logic
            break;
    }
}

Hope it helps!

Upvotes: 1

Related Questions