按下按钮后,我正在制作一个应用程序来调用系统命令。
环境是Mac OS Sierra 10.12.5,Xcode 8.3.3,Objective-C。
这是我的代码:
- (IBAction)ButtonPressed:(UIButton *)sender {
NSString *title = [sender titleForState: UIControlStateNormal];
NSString *plainText = [NSString stringWithFormat:@"%@ button pressed", title];
_statusLabel.text = plainText;
NSPipe *pipe;
pipe = [NSPipe pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath : @"/bin/ls"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-l", @"-a", @"-t", nil];
[task setArguments : arguments];
[task setStandardOutput : pipe];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
NSLog (@"woop! got\n%@", string);
}
我在越狱的iPhone 6(iOS版本10.2)上运行此功能。按下按钮时程序总是崩溃。
崩溃日志是:
2017-07-13 14:08:20.346060 Hello World[2770:532052] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 1' *** First throw call stack: (0x18ae451b8 0x18987c55c 0x18ae45100 0x18b96de6c 0x1000d646c 0x190d2fd30 0x190d2fcb0 0x190d1a128 0x190d2f59c 0x190d2f0c4 0x190d2a328 0x190cfada0 0x1914e475c 0x1914de130 0x18adf2b5c 0x18adf24a4 0x18adf00a4 0x18ad1e2b8 0x18c7d2198 0x190d657fc 0x190d60534 0x1000d69c8 0x189d015b8) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
我不知道'NSInternalInconsistencyException',原因:'无法posix_spawn:错误1'。
另外,我要测试的最终命令是
kill -USR1 `ps ax|grep omm|grep ys|xargs|cut -d ' '-f 1`
但是,上面的命令无法正常工作,我无法继续前进。
有什么想法吗?