2

Let's say I want to run curl -o http://example.com/file.zip via an Objective C app and I want to have a label or text box containing the download status which gets updated while the command is running. Maybe this could be achieved using dispatch_async, but now sure how. Before marking as duplicate, the methods I found, run the command, and after it has finished you get the output. I want to get the output while it's running, kinda like a terminal emulator.

1 Answers1

3

You need to connect a NSPipe to the NSTask using the standardOutput property and register to receive Data Available notifications.

@interface TaskMonitor: NSObject
@property NSPipe *outputPipe;
@end

@implementation TaskMonitor

-(void)captureStandardOutput:(NSTask *)process {

  self.outputPipe = [NSPipe new];
  process.standardOutput = self.outputPipe;

  //listen for data available
  [self.outputPipe.fileHandleForReading waitForDataInBackgroundAndNotify];

  [[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:self.outputPipe.fileHandleForReading queue:nil usingBlock:^(NSNotification * _Nonnull note) {

    NSData *output = self.outputPipe.fileHandleForReading.availableData;
    NSString *outputString = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];

    dispatch_async(dispatch_get_main_queue(), ^{
      // do something with the string chunk that has been received
      NSLog(@"-> %@",outputString);
    });

    //listen again...
    [self.outputPipe.fileHandleForReading waitForDataInBackgroundAndNotify];

  }];

}

@end
Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • Hello I just tried this but has a problem. It runs the whole command, and the output gets NSLogged, as it normaly would in any NSTask, and *after* that I get infinite `->` on the Logs with no data after it, except for the first one where it is like this `-> `. As I said I want one `->` for every piece of data, so I can actually update the UI with it. – Jakeas hacks Jul 31 '17 at 12:09
  • Nvm for some reason wasn't working on the log but worked when I connected it to the UI! – Jakeas hacks Jul 31 '17 at 12:27