Search code examples
iosobjective-cmacososx-elcapitanxcode7.1

Automatically start app after install in Mac OS El Capitan


I'm developing an OSX app which is going to be distributed outside app store. I have archived and created the pkg file but when I install the app it doesnt start automatically. I've to manually start it up from the launchpad. Below is the code which I've added in my appdelegate to show it up in startup.

- (void)installAppIntoLoginItems {

    if (![self appIsInLoginItems]) {
        // Get the LoginItems list.
        LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
        if (loginItemsRef == nil) return;

        // Add the app to the LoginItems list.
        LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, (__bridge CFURLRef)helperAppURL, NULL, NULL);
        if (itemRef) {
            CFRelease(itemRef);
        }
        CFRelease(loginItemsRef);
    }
    else {
        // App is in the LoginItems List
        NSLog(@"App is already in LoginItems List");
    }
}



- (BOOL) appIsInLoginItems {
    // See if the app is currently in LoginItems.
    LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
    // Store away that boolean.
    BOOL isInList = (itemRef != nil);
    // Release the reference if it exists.
    if (itemRef != nil) CFRelease(itemRef);

    return isInList;
}


- (LSSharedFileListItemRef)itemRefInLoginItems {
    LSSharedFileListItemRef itemRef = nil;
    CFURLRef itemUrl = NULL;

    // Get the LoginItems list.
    LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if (loginItemsRef == nil) return nil;
    // Iterate over the LoginItems.
    NSArray *loginItems = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
    for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
        // Get the current LoginItem and resolve its URL.
        LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
//        if (LSSharedFileListItemResolve(currentItemRef, 0, (CFURLRef *) &itemUrl, NULL) == noErr) { //Replacing deprecated method
          if( LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, NULL) == noErr) {
            // Compare the URLs for the current LoginItem and the app.
            if ([(__bridge NSURL*)itemUrl isEqual:helperAppURL]) {
                // Save the LoginItem reference.
                itemRef = currentItemRef;
            }
        }
    }
    // Retain the LoginItem reference.
    if (itemRef != nil) CFRetain(itemRef);
    // Release the LoginItems lists.
    CFRelease(loginItemsRef);

    return itemRef;
}

Do I have to use any other alternative? Is there any sample that I can refer too?


Solution

  • I was able to solve the auto launch app on login issue using SMLoginItemSetEnabled method, and was able to achieve auto launch after install using postinstall script

         if (!SMLoginItemSetEnabled ((__bridge CFStringRef)helperAppBundleIdentifier, YES))
             {
                    NSAlert *alert = [NSAlert alertWithMessageText:@"An error ocurred" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Couldn't add Helper App to launch at login item list."];
                    [alert runModal];
                }