ionic cordova build android/ios --prod --debug
ionic serve
两条命令都可以访问自己生成的CA证书。
但是生成release版本时发现无法访问。
针对已经CA认证过的HTTPS无需做修改。但是第三方生成的需要在工程中做以下修改:
android
cordova v4及以下
project/platforms/android/CordovaLib/src/org/apache/cordova/CordovaWebViewClient.java
cordova v5
project/platforms/android/CordovaLib/src/org/apache/cordova/engine/SystemWebViewClient.java
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);
}
}
ios
project/platforms/ios/Project/Classes/AppDelegate.m
添加:
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
参考文档:http://ivancevich.me/articles/ignoring-invalid-ssl-certificates-on-cordova-android-ios/