Objective-C-执行终端命令
作者:互联网
我想从我的Objective-C项目中运行终端命令.
从终端运行时,我使用:
cd /Users/user/Desktop/project/;ant release
现在我在Objective-C项目中使用了这个:
NSTask *task = [NSTask new];
[task setLaunchPath:@"cd /Users/user/Desktop/project/;ant"];
[task setArguments:[NSArray arrayWithObjects:@"release", nil]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog (@"got\n%@", string);
[string release];
并且在[任务启动]之后;我得到错误:
launch path not accessible
编辑
我试图使用此命令进行检查:
[task setCurrentDirectoryPath:@"/Users/user/Desktop/Czech/"];
[task setLaunchPath:@"/bin/ls"];
而且仍然给我一个警告:
working directory doesn't exist.
解决方法:
您需要以其他方式设置工作目录:
[task setCurrentDirectoryPath:@"/Users/user/Desktop/project"];
然后更改您的setLaunchPath:调用以指向实际可执行文件的位置:
[task setLaunchPath:@"/usr/bin/ant"];
标签:objective-c,linux,macos 来源: https://codeday.me/bug/20191202/2084889.html