[英]How can I determine if my Cocoa Desktop application is on the list of apps to be opened at login?
I am developing a SandBoxed Cocoa Application. 我正在开发Sandboxed可可应用程序。 I have successfully implemented the Launch at login feature by using the Core Foundation function:
我已经使用Core Foundation功能成功实现了“登录时启动”功能:
SMLoginItemSetEnabled
I have based the implementation on This tutorial 我已经根据本教程实现了
But now I need a way to determine if my app is set to be launched at login, so that I can show the button in the appropriate position upon launch. 但是,现在我需要一种方法来确定我的应用程序是否设置为在登录时启动,以便在启动时将按钮显示在适当的位置。 I would expect a similar Core Foundation function to find out if a bundle identifier is on the list of login items, but I didn't find it.
我希望有一个类似的Core Foundation函数可以找出捆绑包标识符是否在登录项目列表中,但是我没有找到它。
Another problem is that, by using this Core Foundation approach, although it is recommended by Apple, my app is still inconsistent with the "Open at Login" tick in my application dock menu. 另一个问题是,尽管使用了Apple建议的这种Core Foundation方法,但我的应用程序仍然与应用程序停靠菜单中的“登录时打开”对勾不一致。
You can do it using the functions in the LSSharedFileList.h header, which is in LaunchServices.framework, which is in CoreServices.framework. 您可以使用LSSharedFileList.h标头(位于LaunchServices.framework和CoreServices.framework中)中的函数来执行此操作。 As far as I can tell, Apple hasn't documented this stuff except in the header comments, but that's probably enough.
据我所知,Apple并没有在标题注释中记录这些内容,但这可能就足够了。 The basic outline is that you first create a
LSSharedFileListRef
using the function LSSharedFileListCreate
for the list type kLSSharedFileListSessionLoginItems
. 基本轮廓是,你首先创建一个
LSSharedFileListRef
使用功能LSSharedFileListCreate
的列表类型kLSSharedFileListSessionLoginItems
。 Then copy a snapshot of the list (which is a CFArrayRef
) using LSSharedFileListCopySnapshot
. 然后使用
LSSharedFileListCopySnapshot
复制列表的快照(它是CFArrayRef
)。 Then for any item in the array, you can get its URL using LSSharedFileListItemCopyResolvedURL
. 然后,对于数组中的任何项目,都可以使用
LSSharedFileListItemCopyResolvedURL
获取其URL。 That last function requires Mac OS X 10.10 or later, while I think the others date back to 10.5. 最后一个功能需要Mac OS X 10.10或更高版本,而我认为其他功能可以追溯到10.5。
By the way, the docs on SMLoginItemSetEnabled
say that it's for setting an embedded helper app as a login item, but it sounds like you're talking about a freestanding app. 顺便说一句,
SMLoginItemSetEnabled
上的文档说这是用于将嵌入式助手应用程序设置为登录项,但是听起来您正在谈论一个独立的应用程序。
Apple's sandbox documentation says: 苹果的沙盒文档说:
With App Sandbox, you cannot create a login item using functions in the LSSharedFileList.h header file.
使用“应用程序沙箱”,您无法使用LSSharedFileList.h头文件中的函数来创建登录项。 For example, you cannot use the function LSSharedFileListInsertItemURL.
例如,您不能使用函数LSSharedFileListInsertItemURL。
But maybe you can still use the shared file list functions on a read-only basis. 但是也许您仍然可以以只读方式使用共享文件列表功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.