Give an NSPipe
or an NSFileHandle
as the task's standardOutput
, and read from that.
NSTask * list = [[NSTask alloc] init];
[list setLaunchPath:@"/bin/ls"];
[list setCurrentDirectoryPath:@"/"];
NSPipe * out = [NSPipe pipe];
[list setStandardOutput:out];
[list launch];
[list waitUntilExit];
[list release];
NSFileHandle * read = [out fileHandleForReading];
NSData * dataRead = [read readDataToEndOfFile];
NSString * stringRead = [[[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"output: %@", stringRead);
Note that if you use a pipe, you have to worry about the pipe filling up. If you provide an NSFileHandle
instead, the task can output all it wants without you having to worry about losing any, but you also get the overhead of having to write the data out to disk.