mirror of
https://github.com/status-im/react-native-webview.git
synced 2026-08-30 19:21:14 +00:00
fix(android): Add SSL error handling for Android WebView (#1450 by @thephpjedi)
Based on PR #668. Fixes #259.
This commit is contained in:
@@ -10,6 +10,7 @@ import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.Manifest;
|
||||
import android.net.http.SslError;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
@@ -26,6 +27,7 @@ import android.webkit.CookieManager;
|
||||
import android.webkit.DownloadListener;
|
||||
import android.webkit.GeolocationPermissions;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.SslErrorHandler;
|
||||
import android.webkit.PermissionRequest;
|
||||
import android.webkit.URLUtil;
|
||||
import android.webkit.ValueCallback;
|
||||
@@ -797,6 +799,50 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||
return this.shouldOverrideUrlLoading(view, url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
|
||||
handler.cancel();
|
||||
|
||||
int code = error.getPrimaryError();
|
||||
String failingUrl = error.getUrl();
|
||||
String description = "";
|
||||
String descriptionPrefix = "SSL error: ";
|
||||
|
||||
// https://developer.android.com/reference/android/net/http/SslError.html
|
||||
switch (code) {
|
||||
case SslError.SSL_DATE_INVALID:
|
||||
description = "The date of the certificate is invalid";
|
||||
break;
|
||||
case SslError.SSL_EXPIRED:
|
||||
description = "The certificate has expired";
|
||||
break;
|
||||
case SslError.SSL_IDMISMATCH:
|
||||
description = "Hostname mismatch";
|
||||
break;
|
||||
case SslError.SSL_INVALID:
|
||||
description = "A generic error occurred";
|
||||
break;
|
||||
case SslError.SSL_NOTYETVALID:
|
||||
description = "The certificate is not yet valid";
|
||||
break;
|
||||
case SslError.SSL_UNTRUSTED:
|
||||
description = "The certificate authority is not trusted";
|
||||
break;
|
||||
default:
|
||||
description = "Unknown SSL Error";
|
||||
break;
|
||||
}
|
||||
|
||||
description = descriptionPrefix + description;
|
||||
|
||||
this.onReceivedError(
|
||||
webView,
|
||||
code,
|
||||
description,
|
||||
failingUrl
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedError(
|
||||
WebView webView,
|
||||
|
||||
Reference in New Issue
Block a user