Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSTask运行脚本_Objective C_Nstask - Fatal编程技术网

Objective c NSTask运行脚本

Objective c NSTask运行脚本,objective-c,nstask,Objective C,Nstask,我正在尝试使用NSTask运行以下命令: $sudo launchctl load /Users/admin/Library/LaunchAgents/com.devdaily.crontabtest.plist 下面是我使用的代码: NSTask *server = [NSTask new]; [server setLaunchPath:@"/bin/launchctl"]; [server setArguments:[NSArray arrayWithObjects:@"load",@"c

我正在尝试使用NSTask运行以下命令:

$sudo launchctl load /Users/admin/Library/LaunchAgents/com.devdaily.crontabtest.plist
下面是我使用的代码:

NSTask *server = [NSTask new];
[server setLaunchPath:@"/bin/launchctl"];
[server setArguments:[NSArray arrayWithObjects:@"load",@"com.devdaily.crontabtest.plist",nil]];
[server setCurrentDirectoryPath:@"/Users/admin/Library/LaunchAgents/"];

NSPipe *outputPipe = [NSPipe pipe];
[server setStandardInput:[NSPipe pipe]];
[server setStandardOutput:outputPipe];

[server launch];
[server waitUntilExit]; // Alternatively, make it asynchronous.
[server release];
但是,由于使用了
sudo
命令,它无法工作。我怎样才能解决这个问题?

不幸的是,你不能。因为没有办法输入密码

但是,您仍然可以通过使用
NSAppleScript
而不是
NSTask
来运行bash命令: 编写一个applescript,如下所示:

do shell脚本
[您的命令]
具有管理员权限

系统将要求您输入管理员密码

示例:

NSDictionary *error = [NSDictionary new]; 
NSString *script =  @"do shell script \"launchctl load /Users/admin/Library/LaunchAgents/com.devdaily.crontabtest.plist\" with administrator privileges";  
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script]; 
if ([appleScript executeAndReturnError:&error]) {
  NSLog(@"success!"); 
} else {
  NSLog(@"failure!"); 
}
阅读苹果公司的建议,并遵循建议


换句话说,不要使用
sudo
或类似的东西。使用
smjobbles()
安装并启用您的launch agent。另请参见。

在非控制台环境中使用
sudo
不是很有用,因为没有简单的方法输入密码(是的,您可以使用标准输入管道,但最好使用OS X的内置升级实用程序)。其次,我猜sudo没有被
sh
加载,因为它没有加载路径。