1

is there a way to get the log from system(); so like when I do system("open com.apple.nike"); I should get Couldn't open application: com.apple.nike. Reason: 8, application disabled or restricted. This will run on my iOs 7 Device

Thanks

EDIT:// This is the new code, but it wont work, I'll get

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

NSString *bundleID = @"com.apple.nike";
    NSTask *task = [[NSTask alloc] init];
                    [task setLaunchPath: @"sudo"];
                    [task setArguments: [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"open %@", bundleID], nil]];

                    NSPipe *pipe= [NSPipe pipe];
                    [task setStandardOutput: pipe];

                    NSFileHandle *file = [pipe fileHandleForReading];

                    [task launch];

                    NSData *data = [file readDataToEndOfFile];

                    NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                    NSLog(@"result: %@", output);
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98

1 Answers1

1

I know this isn't exactly what you asked, but perhaps there's a better way.

If you want to run a command (like open com.apple.nike), I think using NSTask is actually the best way to do that programmatically. NSTask will allow you to run commands just like system(), but has good support for handling the standard output from those commands, without having to do file I/O on the system log file.

For example, here's an example of using NSTask to list directory contents (ls -altr), and capture the output in a NSString:

- (void) listDir {
   NSTask *task = [[NSTask alloc] init];
   [task setLaunchPath: @"/bin/ls"];
   [task setArguments: [[NSArray alloc] initWithObjects: @"-altr", nil]];

   NSPipe *pipe= [NSPipe pipe];
   [task setStandardOutput: pipe];

   NSFileHandle *file = [pipe fileHandleForReading];

   [task launch];

   NSData *data = [file readDataToEndOfFile];

   NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
   NSLog(@"result: %@", output);
}

This will keep the output from your open command separate from any other stuff in the system log file.

NSTask is a private API on iOS, but as is the case with many APIs that exist on OS X, they actually are available on iOS (just don't assume Apple allows them in the App Store!).

To use it, you'll need to download the NSTask.h header, and include it in your project.

Here's an old version, but I bet it still probably works.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Hi, I updated my question, cause it isnt working as expected. My app crashes when I excute the command – David Gölzhäuser Feb 19 '14 at 06:57
  • That's probably because `sudo` isn't installed on your device ... it's not by default. Anyway, I don't think you need it anyway. Running `open` should work as the `mobile` user; you shouldn't need `root` privileges to open an app. Apps normally run as `mobile`. So, instead of your launch path being `sudo`, it should be `open`, or probably `/usr/bin/open`. Then, you've also added a space into your arguments. That's probably not right, either. The arguments are an array, because each argument, which would be separated by spaces on a command line, is a different array element. – Nate Feb 19 '14 at 09:52
  • @DavidG., you're welcome. Including the full path is normally a good idea, for security reasons. Even if you **do** use a `system()` call, it's good practice to fully qualify the command's path. Glad to hear it's working now :) – Nate Feb 19 '14 at 11:03