Custom error page
Xamarin Forms page
webView.Source = "https://example.com";
Xamarin.Android
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace YourAppNamespace.Droid
{
public class HybridWebViewRenderer : WebViewRenderer
{
public HybridWebViewRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetWebViewClient(new CustomWebViewClient());
}
}
}
public class CustomWebViewClient : WebViewClient
{
public override void OnReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
{
string customErrorPage = "<html><body><h1>Page Not Found</h1><p>Oops! Something went wrong.</p></body></html>";
view.LoadData(customErrorPage, "text/html", "UTF-8");
}
public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl)
{
string customErrorPage = "<html><body><h1>Page Not Found</h1><p>Oops! Something went wrong.</p></body></html>";
view.LoadData(customErrorPage, "text/html", "UTF-8");
}
}
}
Xamarin.iOS
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace YourAppNamespace.iOS
{
public class HybridWebViewRenderer : WkWebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.NavigationDelegate = new CustomNavigationDelegate();
}
}
}
public class CustomNavigationDelegate : WKNavigationDelegate
{
public override void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
string customErrorPage = "<html><body><h1>Page Not Found</h1><p>Oops! Something went wrong.</p></body></html>";
webView.LoadHtmlString(customErrorPage, null);
}
public override void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
string customErrorPage = "<html><body><h1>Page Not Found</h1><p>Oops! Something went wrong.</p></body></html>";
webView.LoadHtmlString(customErrorPage, null);
}
}
}