Created
March 8, 2013 14:29
-
-
Save andreashanft/5116781 to your computer and use it in GitHub Desktop.
Handling output of a NSTask
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
NSTask* task = [[NSTask alloc] init]; | |
{ | |
[task setStandardOutput:[NSPipe pipe]]; | |
[task setStandardError:[NSPipe pipe]]; | |
[task setLaunchPath:...]; | |
[task setArguments:...]; | |
[task setTerminationHandler:^(NSTask* task) | |
{ | |
if ([task terminationStatus] == 0) | |
{ | |
NSPipe* outPipe = task.standardOutput; | |
NSData* output = [[outPipe fileHandleForReading] readDataToEndOfFile]; | |
NSString* result = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding]; | |
} | |
else | |
{ | |
NSPipe* errorPipe = task.standardError; | |
NSData* error = [[errorPipe fileHandleForReading] readDataToEndOfFile]; | |
NSString* result = [[NSString alloc] initWithData:error encoding:NSUTF8StringEncoding]; | |
} | |
}]; | |
} | |
[task launch]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment