" ps aux"在iPhone上部署时,命令输出格式错误

时间:2011-08-24 02:12:32

标签: iphone objective-c unix

我试图使用UITableView列出特定进程的详细信息。它在Xcode模拟器上运行时显示完美。但是,在实际设备上部署时会出现格式错误。令人困惑的是,当我在Mac和iPhone上执行“ps aux”命令时,输出的格式看起来相同。

这就是它在模拟器中的样子:

enter image description here

这就是它在真实设备上的样子:

enter image description here

以下是显示这些视图的控制器的代码:

viewDidLoad:

NSLog(@"myString is :%@", myString);
int processID = [myString intValue];

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/ps"];

arguments = [NSArray arrayWithObjects: @"aux", [NSString stringWithFormat:@"%i", processID],nil];

[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
//[task setStandardOutput: pipe];
[task setStandardOutput:pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data
                               encoding: NSUTF8StringEncoding];

//  NSLog(@"%@",string);


NSArray *lines= [string componentsSeparatedByString:@"\n"];

NSString *lastline = [lines objectAtIndex:[lines count]-2];
//  NSLog(@"%@",lastline);




lines2= [lastline componentsSeparatedByString:@" "];
NSLog(@"%@",lines2);
for (int i=0; i<[lines2 count]; i++) {

    if([[lines2 objectAtIndex:i] isEqualToString:@""]){
        [lines2 removeObjectAtIndex:i];
    }

}
for (int i=0; i<[lines2 count]; i++) {

    if([[lines2 objectAtIndex:i] isEqualToString:@""]){
        [lines2 removeObjectAtIndex:i];
    }

}


NSLog(@"Lines 2 is%@",lines2);
NSLog(@"Status is %@",[lines2 objectAtIndex:7]);


self.title = @"Process Info";

label = [[NSMutableArray alloc]init];

[label addObject:@"User:"];
[label addObject:@"Process ID:"];
[label addObject:@"CPU(%):"];
[label addObject:@"MEM(%):"];
[label addObject:@"VSZ:"];
[label addObject:@"RSS:"];
[label addObject:@"TT:"];
[label addObject:@"STAT:"];
[label addObject:@"Time Started:"];
[label addObject:@"Time Elapsed:"];
[label addObject:@"Launch Command:"];

[super viewDidLoad];

的cellForRowAtIndexPath:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
NSString *cellValue = [label objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
cell.textColor = [UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1];
cell.font = [UIFont systemFontOfSize:16.0];


UILabel *label2 =  [[[UILabel alloc] initWithFrame:CGRectMake(120.0, 0, 240.0, 
                                                              tableView.rowHeight)] autorelease]; 


label2.font = [UIFont systemFontOfSize:16.0]; 
NSString *cellValue1 = [lines2 objectAtIndex:indexPath.row];
label2.text = cellValue1;
label2.textAlignment = UITextAlignmentLeft; 
label2.textColor = [UIColor blackColor]; 
label2.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
UIViewAutoresizingFlexibleHeight; 
[cell.contentView addSubview:label2]; 


return cell;

numberOfRowsInSection:

return [label count];

的dealloc:

 [myString release];
[arguments release];
//[lines2 release];
[ResultStringID release];
[values release];
[label release];
[super dealloc];

Mac上“ps aux”命令的输出:

enter image description here

iPhone上“ps aux”命令的输出(通过SSH shell):

enter image description here

1 个答案:

答案 0 :(得分:0)

无法保证iOS上的ps与其他平台上的内容完全相同。没有允许用户在iOS设备上访问ps命令的UI,所以你很幸运,根本就有ps命令。尝试记录命令的输出以查看您获得的内容,但如果没有那么多,请不要感到惊讶。