问题
In ionic simulator all http and https working fine but in real device release version its stop working.
Many people adviced to add SSL certificate for release version but I dont know how to add this ?
I have tries all this to make https request ?
<access origin="*"/>
<access origin="*"/>
<allow-navigation href="*"/>
<allow-intent href="*" />
Also added whitelist plugin but not working.
Also tried this but not working
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
回答1:
Yes, this is the most common issue raised by all the developers.. Finally I found the answer and here it is..
SSL certificate is enough for a website to connect via HTTPS but coming to hybrid Apps like App build on ionic, SSL itself is not enough and you need a code signing certificate which costs few hundred bucks..
Other alternative is using a self signed certificate which you ca create FREE, here also you can able to connect to ionic web version via HTTPS but again after compiling, the apk file get halted to connect to DB server..
Here is the trick where you can add the HTTPS port or HTTP api link in the services using self signed certification..
Exactly follow the commands:
- ionic serve
- ionic build
- ionic build --prod
- cordova platform add android
After this, stop compiling and relax .. Now you will find a folder named platform as the path is as follows project/platforms/android/CordovaLib/src/org/apache/cordova/CordovaWebViewClient.java
DO THE FOLLOWING MODIFICATION AS IT IS
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
final String packageName = this.cordova.getActivity().getPackageName();
final PackageManager pm = this.cordova.getActivity().getPackageManager();
ApplicationInfo appInfo;
try {
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
// debug = true
handler.proceed();
return;
} else {
// debug = false
// THIS IS WHAT YOU NEED TO CHANGE:
// 1. COMMENT THIS LINE
// super.onReceivedSslError(view, handler, error);
// 2. ADD THESE TWO LINES
// ---->
handler.proceed();
return;
// <----
}
} catch (NameNotFoundException e) {
// When it doubt, lock it out!
super.onReceivedSslError(view, handler, error);
}
}
THEN FINALLY
- cordova build --release android
After this, the usual commands jarsiner and zipalign
you apk should run in HTTPS port in SSL withh your self signed certificate..
来源:https://stackoverflow.com/questions/40147383/how-add-android-ssl-certificate-for-https-request-in-ionic