Methods are NSWorkspacereally designed to run independent applications; use NSTaskto "run another program as a subprocess and ... control the execution of programs" according to the documents.
Here is an easy way to run the executable and return to its standard output - it blocks waiting for completion:
+ (NSString *) runCommand:(NSString *)atPath withArguments:(NSArray *)arguments
{
NSTask *task = [NSTask new];
NSPipe *pipe = [NSPipe new];
[task setStandardOutput:pipe];
[task setLaunchPath:atPath];
if(arguments != nil)
[task setArguments:arguments];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
if ([task terminationStatus] != 0)
return nil;
if (data == nil)
return nil;
return [NSString stringWithUTF8Data:data];
}
You may not want to block, especially if this is your main UI thread, put standard input, etc.
source
share