Ok. There are several questions on stack overflow about this. This question was the only question comes closest to mines, but it uses notifications.
The code is very simple. Create a new empty Mac OSX project and just paste the following code in the applicationDidFinishLaunching:
method. It supposed to get the path of any executable file (in this case GIT).
NSTask *aTask = [[NSTask alloc] init]; NSPipe *outputPipe = [NSPipe pipe]; NSPipe *errorPipe = [NSPipe pipe]; [aTask setStandardOutput: outputPipe]; [aTask setStandardError: errorPipe]; [aTask setArguments:[NSArray arrayWithObject:@"which"]]; [aTask setLaunchPath:@"/usr/bin/git"]; NSFileHandle *outputFileHandler = [outputPipe fileHandleForReading]; NSFileHandle *errorFileHandler = [errorPipe fileHandleForReading]; [aTask launch]; [aTask waitUntilExit]; // Task launched now just read and print the data NSData *data = [outputFileHandler readDataToEndOfFile]; NSString *outPutValue = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSData *errorData = [errorFileHandler readDataToEndOfFile]; NSString *errorValue = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding]; NSLog(@"Error value: %@",errorValue); NSLog(@"Output Value: %@",outPutValue);
This code sets up two reading pipes and runs one command: which git
.
If i run this in XCode i get this results corretly:
Error value: "" Output Value: /usr/bin/git
If i go to my build/Products/Debug folder and double click the executable file, i get this message printed on the console app:
Question: So, what is really the problem here? please just dont make an alternative solution... I also want to know what the problem is.. thanks.