Last active
December 11, 2015 07:51
-
-
Save iburlakov/1d0d194cf07a35505a25 to your computer and use it in GitHub Desktop.
nstask output 2 console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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