我怎么能做一个if语句:
if(the output of a nstask is equal to a specific string){
//do stuff over here
}
我正在运行NSTask
,并且它在NSLog中输出了来自它的数据,但我怎么能不在那里显示它,而是将其存储为NSString或类似的东西
这就是我的任务
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/csrutil"];
[task setArguments:@[ @"status" ]];
[task launch];
非常感谢任何帮助:)
答案 0 :(得分:2)
您需要TaskStackBuilder
和pipe
才能阅读结果。
写一个方法
file handle
并将其命名为
- (NSString *)runShellScript:(NSString *)cmd withArguments:(NSArray *)args
{
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:cmd];
[task setArguments:args];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
NSData *data = [file readDataToEndOfFile];
return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}