NSTaskを使う


   NSTask *task = [[NSTask alloc] init];
   NSPipe *pipe = [[NSPipe alloc] init];
   
   //コマンド入力
   [task setLaunchPath:@"/bin/ls"];
   [task setStandardOutput:pipe];
   NSArray *args = [NSArray arrayWithObject:@"../../"];
   [task setArguments:args];

   //実行
   [task launch];
   [task waitUntilExit];
      
   //実行結果のデータ取得
   NSFileHandle *handle = [pipe fileHandleForReading];
   NSData *data = [handle  readDataToEndOfFile];
   NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    
   NSLog(string);
    
   [task release];
   [pipe release];

最終更新:2012年09月03日 11:47