在Mac OS El Capitan中安装后自动启动应用程序

时间:2016-02-19 05:49:16

标签: ios objective-c macos osx-elcapitan xcode7.1

我正在开发一个OSX应用程序,它将在app store外部发布。我已经存档并创建了pkg文件但是当我安装应用程序时它不会自动启动。我要从启动板手动启动它。以下是我在appdelegate中添加的代码,以便在启动时显示它。

- (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;
}

我是否必须使用其他替代方案?有没有我可以参考的样品?

2 个答案:

答案 0 :(得分:0)

您可以使用启动代理:

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

http://launchd.info

我认为将其作为登录项的优点是用户可能会弄​​清楚如何自行删除它。如果你使用LaunchAgent,你可能必须在登录时建立一个&#34;启动#34;您的应用中的偏好选项。

答案 1 :(得分:0)

我能够使用SMLoginItemSetEnabled方法在登录问题上解决自动启动应用程序,并且能够在安装后使用postinstall脚本实现自动启动

     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];
            }
相关问题