For Sandboxed apps, to create a launch item, Apple suggest you use LSRegisterURL(..)
and SMLoginItemSetEnabled(..)
along with a helper tool. I've set up everything how I want it but I would like a way (not storing a preference value) to get the status of "if it is registered". Basically a way to perform the same action as SMLoginItemGetEnabled(...)
would.
EDIT: Here is my final code thanks to Rob Keniger's answer:
- (BOOL)startAtLogin {
NSDictionary *dict = (NSDictionary*)SMJobCopyDictionary(kSMDomainUserLaunchd,
CFSTR("com.yourcompany.app"));
BOOL contains = (dict!=NULL);
[dict release];
return contains;
}
The accepted answer was not working for me. This blog says "// Note: Sandbox issue when using SMJobCopyDictionary()
" which must be the problem. So if you're having trouble using the accepted answer here in the sandbox, give this method a try. Working for me.
All credit to Mike Cohen for this. See his blog post for more:
http://blog.mcohen.me/2012/01/12/login-items-in-the-sandbox/
-(BOOL)appIsPresentInLoginItems
{
NSString *bundleID = @"com.madebynotion.myLoginHelper";
NSArray * jobDicts = nil;
jobDicts = (NSArray *)SMCopyAllJobDictionaries( kSMDomainUserLaunchd );
// Note: Sandbox issue when using SMJobCopyDictionary()
if ( (jobDicts != nil) && [jobDicts count] > 0 ) {
BOOL bOnDemand = NO;
for ( NSDictionary * job in jobDicts ) {
if ( [bundleID isEqualToString:[job objectForKey:@"Label"]] ) {
bOnDemand = [[job objectForKey:@"OnDemand"] boolValue];
break;
}
}
CFRelease((CFDictionaryRef)jobDicts); jobDicts = nil;
return bOnDemand;
}
return NO;
}
I think you could use SMCopyAllJobDictionaries(kSMDomainUserLaunchd)
to get an array containing dictionaries for all the currently active launchd
jobs.
According to the docs, calling SMLoginItemSetEnabled()
immediately starts the job in question, so if your login task is not in the list returned by SMCopyAllJobDictionaries()
then you can probably assume it's not set to run at login.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With