When there is an internet connection, the WebView opens a url. But if there isn't, it opens a html file. And Xamarin Forms
All you need to do is swap the type of WebViewSource
that you are using and assign the proper properties to it wether you are connected or not.
So for a Forms' WebView, if you are "Internet" connected, create a UrlWebViewSource
and assign the property Url, but if not, create a HtmlWebViewSource
and assign the BaseUrl
to either NSBundle.MainBundle.BundlePath
or file:///android_asset/
for static app bundled resources, or your custom cache directory.
Something like this:
WebViewSource webViewSource;
if (InternetConnected)
{
webViewSource = new UrlWebViewSource { Url = "https://stackoverflow.com" };
}
else
{
string baseUrl = cacheDir;
webViewSource = new HtmlWebViewSource { BaseUrl = baseUrl, Html = cachedHtml };
}
webView.Source = webViewSource;