0

我对iOS课程没有太多了解。我正在构建一个需要 SSL 固定的 iOS 应用程序(Ionic 3)。大部分 google 的例子都是基于 swift 的。我可以知道这些步骤,或者有人可以提供一些有关 iOS SSL 固定的链接吗?

PS:我的服务器中已经有证书。此外,我已经使用 Android 的网络安全配置完成了 SSL 固定。参考https://developer.android.com/training/articles/security-config。它工作正常。

提前致谢。

4

1 回答 1

1

Ionic 5.4.15 版本解决方案。

要在 ionic 中启用 SSL 固定,请在根文件夹中创建一个目录,例如“证书”,并将所有证书放在此文件夹中。重要提示:此文件夹中的所有证书都必须具有后缀 .cer!!!

之后修改根项目目录中的 angular.json 将此部分附加到“资产”数组的所有出现。

{   
"glob": "**/*", 
"input": "certificates",    
"output": "certificates"
}

然后删除根项目中的 www 目录并运行“ionic build”,它将在您的 www 文件夹中生成新的子目录“certificates”

在 Typescript 中使用证书:

我正在使用 ionic-native http 和 cordova-advanced-http-plugin

安装:

ionic cordova plugin add cordova-plugin-advanced-http
npm install @ionic-native/http

在您的根目录 xyz.module.ts 文件中导入:

import { HTTP } from '@ionic-native/http/ngx';

将其附加到提供者:

 providers: [
    StatusBar,
    SplashScreen,
    **HTTP**,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],

在您的页面/组件中导入 xyz.ts 文件:

import { HTTP } from '@ionic-native/http/ngx';

在构造函数中声明:

constructor(private http: HTTP) {}

在提出任何请求之前固定证书:

  async ngOnInit() {
    await this.platform.ready();
    this.advHttp.setServerTrustMode('pinned').then((res: any) => {
    }, (error) => {
      this.helpers.showError(error);
    });
    this.advHttp.setRequestTimeout(5);
  }

现在一切就绪,可以使用 https 请求了!文档: https ://ionicframework.com/docs/native/http

于 2020-01-21T11:24:36.403 回答