下面是如何在iPhone非官方SDK程序中调用外部命令的方法。

 

 

  1. - (NSString *)executeCommand: (NSString *)cmd
  2. {
  3.     NSString *output = [NSString string];
  4.     FILE *pipe = popen([cmd cStringUsingEncoding: NSASCIIStringEncoding], "r");
  5.     if (!pipe) return;
  6.  
  7.     char buf[1024];
  8.     while(fgets(buf, 1024, pipe)) {
  9.     output = [output stringByAppendingFormat: @"%s", buf];
  10. }
  11.  
  12. pclose(pipe);
  13. return output;
  14. }
  15.  
  16. NSString *yourcmd = [NSString stringWithFormat: @"your command"];
  17. [self executeCommand: yourcmd];