There's a working solution on the Capacitor github https://github.com/ionic-team/capacitor/issues/3707#issuecomment-713360155
IMPORTANT: This should be for debugging only, your app might get rejected if you leave this code in a production build. The code ignores SSL errors so shouldn't be in any live code.
For Capacitor v3 I import these lines:
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
Then add an override to onStart() in my app's MainActivity.java
@Override
public void onStart() {
super.onStart();
if (BuildConfig.DEBUG) {
this.bridge.getWebView().setWebViewClient(new BridgeWebViewClient(this.bridge) {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
}};
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } });
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
For Capacitor v2, this may work - but if not the code can be moved from onStart() to onCreate(Bundle savedInstanceState) where I saw it working before upgrading to v3.
I run it from the CLI with
ionic capacitor run android -l --host=0.0.0.0 --consolelogs --external --ssl
--https
flag working? – Cantor