After the comments here is a working example built in Xcode. The runCommand
method is based on this answer updated and with optional use of waitUntilExit
so you can see not using it does not freeze the app.
@implementation AppDelegate
// Arguments:
// atPath: full pathname of executable
// arguments: array of arguments to pass, or nil if none
// Return:
// the standard output, or nil if any error
//
// This method blocks the caller until the called command completes
// (due to both readDataToEndOfFile & waitUntilExit)
// If this an issue either run on a thread or use the asynchronous read alternatives to readDataToEndOfFile
// If there is no need to check terminatation status the waitUntilExit and terminationStatus can be skipped
// by defining RUN_COMMAND_CHECK_TERMINATION as 0
#define RUN_COMMAND_CHECK_TERMINATION 0
+ (NSString *) runCommand:(NSString *)atPath withArguments:(nullable NSArray *)arguments
{
NSTask *task = [NSTask new];
NSPipe *pipe = [NSPipe new];
task.standardOutput = pipe; // pipe standard output
task.launchPath = atPath; // set path
if(arguments)
task.arguments = arguments; // set arguments
[task launch]; // execute
NSData *data = pipe.fileHandleForReading.readDataToEndOfFile; // read standard output
#if RUN_COMMAND_CHECK_TERMINATION
[task waitUntilExit]; // verify process has exited to prevent terminationStatus faulting
// must do this *after* read otherwise if pipe fills wait can block forever
if (task.terminationStatus != 0) // check termination status & have output
return nil;
#endif
if (data == nil) // check termination status & have output
return nil;
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // return stdout as string
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *stringRead = [AppDelegate runCommand:@"/bin/ls" withArguments:@[@"/"]];
NSLog(@"PATH: %@", stringRead);
NSArray *outputList = [stringRead componentsSeparatedByString:@"\n"]; // Note: will produce an empty last line, removing that is left as an exercise
NSLog(@"Lines: %@", outputList);
}
@end
If your code is freezing either its down to something strange with HBLogDebug
(as the above uses NSLog
) or something else in your code which you'll have to ferret out.
HTH