2011-08-01 10 views
1

私のプログラムでターミナルコマンドを実行したい。 コマンドは次のようになります。NSTaskでターミナルコマンドを実行する

cd /path/to/file/; ./foo HTTPProxy 127.0.0.1 

それはsystem()で動作しますが、私はNSTaskを使用する場合、それは動作しません。

system("cd /path/to/file/; ./foo HTTPProxy 127.0.0.1"); 

正常に動作しますが、

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/path/to/file/./foo"]; 

NSPipe *pipe = [NSPipe pipe]; 
[task setStandardOutput:pipe]; 
NSFileHandle *file = [pipe fileHandleForReading]; 

[task setArguments:[NSArray arrayWithObjects:@"HTTPProxy 127.0.0.1", nil]]; 
[task launch]; 

NSData *data = [file readDataToEndOfFile]; 
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 
NSLog(string); 

はしていません。 出力:

Command-line option 'HTTPProxy 127.0.0.1' with no value. Failing. 

アイデアはありますか?

答えて

2

今、私はそれを持っていると思う:あなたが現在設定しようとする可能性が

[task setArguments:[NSArray arrayWithObjects:@"HTTPProxy", @"127.0.0.1", nil]]; 

ものは、コマンドライン...

OLD ANSWERからあなたの呼び出しで別の引数です実行のディレクトリ:

– setCurrentDirectoryPath: 

これは基本的に01の効果ですコードのsystemバージョンの。

+0

いいえ、同じエラー... – ahee

+0

はいありがとうございました。現在作業中です! – ahee

関連する問題