Skip to content

Instantly share code, notes, and snippets.

@iburlakov
Last active December 11, 2015 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iburlakov/1d0d194cf07a35505a25 to your computer and use it in GitHub Desktop.
Save iburlakov/1d0d194cf07a35505a25 to your computer and use it in GitHub Desktop.
nstask output 2 console
//
// main.m
// sandbox-nstask
//
// Created by Ivan Burlakov on 11/12/15.
// Copyright © 2015 Ivan Burlakov. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TaskLauncher.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
[[[TaskLauncher alloc] init] start];
NSLog(@"The End");
}
return 0;
}
//
// TaskLauncher.h
// sandbox-nstask
//
// Created by Ivan Burlakov on 11/12/15.
// Copyright © 2015 Ivan Burlakov. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TaskLauncher : NSObject
- (void)start;
@end
//
// TaskLauncher.m
// sandbox-nstask
//
// Created by Ivan Burlakov on 11/12/15.
// Copyright © 2015 Ivan Burlakov. All rights reserved.
//
#import "TaskLauncher.h"
@implementation TaskLauncher
- (void)start {
NSTask *t = [[NSTask alloc] init];
[t setLaunchPath:@"/bin/echo"];
[t setArguments:@[@"Hello World!"]];
NSPipe *outputPipe = [NSPipe pipe];
[t setStandardOutput:outputPipe];
[t setStandardError:outputPipe];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(readCompleted:)
name:NSFileHandleReadToEndOfFileCompletionNotification
object:[outputPipe fileHandleForReading]];
[[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
[t launch];
[t waitUntilExit];
}
- (void)readCompleted:(NSNotification *)notification {
NSData *data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *string = [[NSString alloc] initWithData: data
encoding: [NSString defaultCStringEncoding]];
NSLog(@"OUTPUT/ERROR: %@", string);
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSFileHandleReadToEndOfFileCompletionNotification
object:[notification object]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment