如何在/ Library / LaunchAgents下创建.plist文件

时间:2017-11-09 15:25:38

标签: objective-c macos launchd launch-agent

我试图通过Apple Doc开发macOS的启动代理

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

我的一个要求是代理应该适用于所有用户。我从上面的文档中理解的是,我必须将我的.plist放在" / Library / LaunchAgents"文件夹中。

当我尝试以编程方式创建此文件时,下面的代码没有任何反应。

NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
[plist setObject:@"test" forKey: @"test 1"];
NSString *userLaunchAgentsPath = [[NSString alloc] initWithFormat:@"%@",  @"/Library/LaunchAgents/com.xxx.agent.plist"];
    [plist writeToFile:userLaunchAgentsPath atomically:YES];

可能原因是特权问题。你对解决这个问题有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对于权限,plist应该由root拥有,如果您希望应用程序以其他用户身份运行,您可以通过在plist中提供用户名/密码来轻松完成。您的应用可能没有以root用户身份运行。