Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMLoginItemSetEnabled(...) GET counterpart

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;
}
like image 778
Alex Zielenski Avatar asked Nov 21 '11 23:11

Alex Zielenski


2 Answers

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;
}
like image 180
ck_ Avatar answered Oct 14 '22 13:10

ck_


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.

like image 35
Rob Keniger Avatar answered Oct 14 '22 13:10

Rob Keniger