I have it working. However, initially it appears to be waiting for input. How can I read what was output before it stops and is waiting for input?
task starts
stdout should post data for "sftp> " but nothing happens, its waiting for input
If I write "\n" to stdin
stdout notifies me of data available which ends up being "sftp> \n"
Here is how it's implemented:
[self.task = [[NSTask alloc] init];
[self.task setLaunchPath:executablePath];
[self.task setArguments: args];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(terminatedNotification:) name:NSTaskDidTerminateNotification object:self.task];
//reading
self.stdoutPipe = [NSPipe pipe];
self.task.standardOutput = self.stdoutPipe;
[self.stdoutPipe.fileHandleForReading waitForDataInBackgroundAndNotify];
//error
self.stderrorPipe = [NSPipe pipe];
self.task.standardError = self.stderrorPipe;
[self.stderrorPipe.fileHandleForReading waitForDataInBackgroundAndNotify];
/*
reading works perfect until I uncomment this section
//writing
self.stdinPipe = [NSPipe pipe];
self.task.standardInput = self.stdinPipe;
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataAvailable:) name:NSFileHandleDataAvailableNotification object:self.stdoutPipe.fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataAvailable:) name:NSFileHandleDataAvailableNotification object:self.stderrorPipe.fileHandleForReading];
PseudoTTY.app
andasynctask.m
sample code over at: http://cocoadev.com/wiki/NSTask Please post a SSCCE, a Short, Self Contained, Correct (Compilable), Example. Thanks. – Jan 25 '13 at 15:35