2013-06-27 16 views
7

Ok. Su questo argomento ci sono diverse domande sullo stack overflow. This question was the only question arriva più vicino alle miniere, ma utilizza le notifiche.Percorso di avvio NSTask non accessibile. Funziona in Xcode. Errore mostrato in XCode

Il codice è molto semplice. Crea un nuovo progetto Mac OSX vuoto e incolla il seguente codice nel metodo applicationDidFinishLaunching:. Doveva ottenere il percorso di qualsiasi file eseguibile (in questo caso 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); 

Questo codice imposta due tubi di lettura e corre un solo comando: which git.

se corro questo in XCode ottengo questo si traduce corretly:

Error value: "" 
Output Value: /usr/bin/git 

Se vado al mio/Prodotti cartella build/Debug e fare doppio clic sul file eseguibile, ottengo questo messaggio stampato sulla console app :

enter image description here

domanda: Quindi, ciò che è veramente il problema qui? per favore non fare una soluzione alternativa ... voglio anche sapere qual è il problema .. grazie.

risposta

13

OK la risposta è in overflow dello stack, ma è suddivisa in diverse domande.

La domanda è stata posta qui ->Commands with NSTask e qui ->NSTask launch path not accessible così

Ma le loro risposte a partire da questa data arent chiaro da quale fosse il problema. È solo dopo aver letto la domanda da NSTask not picking up $PATH from the user's environment (il titolo della domanda era fuorviante) e con queste due risposte NSTask not picking up $PATH from the user's environment e Find out location of an executable file in Cocoa che ho realizzato la soluzione.

Sembra che questo è sull'impostazione sia NS Attività o shell dell'utente (ad esempio, ~/.bashrc) in modo che il corretto ambiente ($ PATH) è visto da NSTask.

Soluzione:

[task setLaunchPath:@"/bin/bash"]; 
NSArray *args = [NSArray arrayWithObjects:@"-l", 
       @"-c", 
       @"which git", //Assuming git is the launch path you want to run 
       nil]; 
[task setArguments: args]; 

Tuttavia ciò presuppone shell dell'utente è sempre bash e sarà sicuro per gli altri. Risolvi questo determinando la shell.

NSDictionary *environmentDict = [[NSProcessInfo processInfo] environment]; 
NSString *shellString = [environmentDict objectForKey:@"SHELL"];