The following code doesn’t work for device Its not going into the while loop on device but it runs on the simulator.
int status;
char value[1024] = "abcd";
FILE *fp = popen("openssl enc -aes-128-cbc -k secret -P -md sha1 2>&1", "r");
if (fp == NULL)
exit(1); // handle error
int i=0;
NSString *strAESKey;
while (fgets(value, 1024, fp) != NULL)
{
i++;
if(i==2)
{
strAESKey=[NSString stringWithFormat:@"%s",value];
break;
}
}
status = pclose(fp);
if (status == -1)
{
/* Error reported by pclose() */
}
else
{
/* Use macros described under wait() to inspect `status' in order
to determine success/failure of command executed by popen() */
}
Where am I going wrong?
The iOS application sandbox forbids use of the
forkfunction, whichpopenuses. The simulator doesn’t use the sandbox, but devices do.You will need to use the openssl library directly instead of using the command-line program. The iOS public API doesn’t include the openssl library, so you’ll need to build a static library yourself. You can find some help doing this by searching. I’d start with this blog post.