简体   繁体   中英

Check whether app is Login Item under OS X Yosemite

In my Mac app, I've been using SMCopyAllJobDictionaries() to check whether the app will be launched automatically at login. Basically, I get an array of all the login items and check whether my Bundle ID is in the array.

This function call works until Mavericks, but is deprecated in Yosemite . According to Apple,

This routine is deprecated and will be removed in a future release. There will be no provided replacement.

However, how can I do the same job in Yosemite? I do have to check whether my app is in the list of login items, in order to show a check box properly. I couldn't find relevant documentation from Apple.

After some research it appears that there isn't an easy answer to this, period. After testing multiple apps including F.lux and BetterSnapTool, I've been able to easily desynch their user interfaces from the system preferences. If I enable "launch on start up" in any of these apps, then remove them from the system preferences log in items section, then relaunch them, their interfaces still think they are set to launch on start up. Interacting with their checkboxes does nothing, as the apps try to remove themselves from the list they no longer belong to and a second click is required to do anything.

To me this signifies that they keep their own internal state as a BOOL and save it between launches and that there simply isn't a way to get the list to synch with as of Yosemite. If someone knows otherwise I'll give them the bounty.

As of WWDC 2017, Apple engineers have stated that this is still the preferred API to use.

However, using this API will cause your build to fail. Don't turn off all deprecated function warnings. Instead, to enable your app to compile, wrap SMCopyAllJobDictionaries with the following:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  CFArrayRef  cfJobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd );
#pragma clang diagnostic pop

If this issue is important to your app and you'd like Apple to provide a clean solution, please file a radar; this helps Apple engineers determine priorities of work items.

I'm using LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL) for a similar purpose. See Nick Moore's answer to How do you make your App open at login? for an example.

Note that this works for login items, but might not work for launchd jobs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM