如何在沙盒应用程序中添加调度功能?

6

我需要在我的应用程序中添加调度功能,以便我的应用程序可以在指定的时间和日期自动启动。我通过参考计划定时作业来完成这个功能。我已经将用于调度的plist文件放置在/Users/username/Library/LaunchAgents中。它在非沙盒化应用程序中运行良好,但在沙盒化应用程序中不起作用。我无法将调度文件添加到路径/Users/username/Library/LaunchAgents中。它给了我错误信息。

Error Domain=NSCocoaErrorDomain Code=513 "“com.sample.schedule.plist” couldn’t be copied because you don’t have permission to access “LaunchAgents”." UserInfo=0x100114f10 {NSSourceFilePathErrorKey=/Users/username/Library/Developer/Xcode/DerivedData/SchedulingInSandbox-cernhnigkuqhehbndryxlekpiiiu/Build/Products/Debug/SchedulingInSandbox.app/Contents/Resources/com.sample.schedule.plist, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=/Users/username/Library/LaunchAgents/com.sample.schedule.plist, NSFilePath=/Users/username/Library/Developer/Xcode/DerivedData/SchedulingInSandbox-cernhnigkuqhehbndryxlekpiiiu/Build/Products/Debug/SchedulingInSandbox.app/Contents/Resources/com.sample.schedule.plist, NSUnderlyingError=0x1001132d0 "The operation couldn’t be completed. Operation not permitted"}

我已经在权限设置中添加了LaunchAgents路径。
<key>com.apple.security.temporary-exception.files.home-relative-path.read-write</key>
<array>
    <string>/Library/LaunchAgents/</string>
</array>

但我仍然无法将调度plist添加到LaunchAgents目录中。我是否做得正确或者有其他方法在沙盒应用程序中进行调度。
编辑1: 如果我手动在 /Library/Containers/LaunchAgents 中创建文件夹并将plist放在那里,我会收到此错误:
Could not open job overrides database at: /private/var/db/launchd.db/com.apple.launchd.peruser.501/overrides.plist: 1: Operation not permitted
launch_msg(): Socket is not connected
1个回答

1
尝试将您的工作 plist 放入中。
/Users/<username>/Library/Containers/<my app identifier>/Data/Library/LaunchAgents/<my app identifier>.<somename>.plist

这将是您隔离应用程序的数据容器。
在我的 Mac 上查看,我发现 FaceTime 有两个计划任务 plist,您可以将其用作示例。

但是容器中没有LaunchAgents目录。我需要自己创建吗? - Aravindhan
一旦您被沙盒化,大多数路径查找API应该会给您正确的相对路径。例如,如果您使用NSHomeDirectory,它将把您放置在应用程序的容器中。然后,您只需复制或创建您的plist即可。 - macshome
我创建了目录并将plist文件放在那里。请查看更新的问题。 - Aravindhan
你是否在应用程序容器的 Containers 目录或 Library 文件夹内创建了 LaunchAgents 文件夹? - macshome
我创建了这个路径:/Users/username/Library/Containers/com.sample.example/Data/Library/LaunchAgents/schedule.plist - Aravindhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接