Harsha Garg
Harsha Garg

Reputation: 11

Ionic- self-signed https certificate exception on ios

We are developing an hybrid mobile app in ionic to get data from server and display it in app. We have a self signed certificate to query to our server. We have placed the .cer file in xcode project->target for iOS. It is giving exception for https query using the certificate.

Our dev environment versions are as below: ionic version 3.20.0

we are using following plugin for http. https://ionicframework.com/docs/native/http/

Ionic code is as below

let head = {
      'Access-Control-Allow-Origin' : '*',
      'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT',
      'Accept':'application/json',
      'content-type':'application/json'
    }


this.http.enableSSLPinning(true).then(data =>{
  console.log("SSL success");
 })
 .catch(error =>{
   console.log("SSL error");
 });
 this.http.acceptAllCerts(true);
 this.http.setDataSerializer("json");
    this.http.post(this.getProfileURL, requestData, head)
  .then(data => {
console.log("success");
  resolve(JSON.parse(data.data));
  })
  .catch(error => {
   reject(error.error);

  });
});

THis code works on Android and not in iOS It is giving below error: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

Questions: 1. Is the location of the certificate correct? (We have placed the .cer file in xcode project->target for iOS.) 2. Which certificate type should we use - .cer, .der, .pem? 3. How do I resolve NSInvalidArgumentException?

Any help on this will be appreciated. Thanks.

Upvotes: 1

Views: 1493

Answers (1)

Jose Enrique
Jose Enrique

Reputation: 59

I was having this issue and my problem was the format of the certificate. Initially the format was PEM and in iOS you need to use the DER format.

If this is your case, you can easily convert your certificate using the following open_ssl command:

openssl x509 -outform der -in myCertificate.cer -out myFormattedCertificate.cer

Hint: You can easily check if your certificate is in PEM format if you open it in a text editor such as Sublime Text. You will see that starts with something like this: "-----BEGIN CERTIFICATE-----"

Upvotes: 0

Related Questions