2

I'm trying to build an app for the iPhone (or iPad, for that matter) in which I want to run some shell commands. The iPhone on which I want it to work is not jailbroken.

The system() command seems to work in executing a shell command, but output and input is of course still a problem. I learned that NSTask may be used for these kinds of things and that it's present, though not documented. (Including the Mac NSTask.h seems to work perfectly)

Now the problem is that when executing this code:

NSTask *task;
task=[[NSTask alloc] init];
task.launchPath=@"/usr/bin/ls";
task.arguments=[NSArray array];
NSPipe *pipe=[NSPipe pipe];
task.standardOutput=pipe;
task.standardError=pipe;
NSFileHandle *output=pipe.fileHandleForReading;
pipe=[NSPipe pipe];
task.standardInput=pipe;
[task launch];

where pipe is for future I/O, it generates an error reading:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'

I already tried quite a few possible paths for the unix bin directory but either I can't find it or my app doesn't have permissions to list files. -.-

How do I execute shell commands on an iPhone while having control over both input and output of that command?

tomsmeding
  • 916
  • 7
  • 25
  • if it's not jailbroken then it won't work. Additionally, this is probably the singularly worst way to list the contents of a directory. see http://stackoverflow.com/questions/499673/getting-a-list-of-files-in-a-directory-with-a-glob – Anya Shenanigans Mar 28 '13 at 17:47
  • The `ls` is nust an example, it could have been any other command. But why only jailbroken? Why can't I just execute a shell command? – tomsmeding Mar 28 '13 at 18:29
  • Because it's not permitted in iOS. Anything that involves the direct creation of another process will be denied by security policy. – Anya Shenanigans Mar 28 '13 at 19:08
  • Well... That fails, I guess :) Post that as an answer and if there isn't anyone else able to help, I'll accept it. – tomsmeding Mar 28 '13 at 19:12

1 Answers1

5

Any attempts to directly create a secondary process in iOS will be denied by the security policy unless you are running on a jailbroken phone.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122