Cocoa应用程序如何将自身添加为全局登录项?

时间:2011-03-27 12:14:36

标签: cocoa macos login

我试过

LSSharedFileListRef globalLoginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL);
if (globalLoginItems) {
    LSSharedFileListItemRef ourLoginItem = LSSharedFileListInsertItemURL(globalLoginItems,
                                                                         kLSSharedFileListItemLast,
                                                                         NULL, NULL,
                                                                         (CFURLRef)[[NSBundle mainBundle] bundleURL], 
                                                                         NULL, NULL);
    if (ourLoginItem) {
        CFRelease(ourLoginItem);
    } else {
        NSLog(@"Could not insert ourselves as a global login item");
    }

    CFRelease(globalLoginItems);
} else {
    NSLog(@"Could not get the global login items");
}

当我构建并运行应用程序时,LSSharedFileListInsertItemURL()刚刚返回NULL。我还需要做些什么吗?某种授权?

注意:此处的用例适用于全局登录项,即使用kLSSharedFileListGlobalLoginItems而不是kLSSharedFileListSessionLoginItems。

3 个答案:

答案 0 :(得分:5)

我得到了这个工作。在将应用程序插入登录项之前,我所要做的就是添加这些行:

AuthorizationRef auth = NULL; 
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth);
LSSharedFileListSetAuthorization(globalLoginItems, auth);

LSSharedFileListSetAuthorization的文档说我们必须为此获得正确的system.global-login-items,但它仍然有效!

但如果用户不是管理员,则会失败。为了使它能够工作,你必须这样做:

AuthorizationItem right[1] = {{"system.global-login-items.", 0, NULL, 0}};
AuthorizationRights setOfRights = {1, right};
AuthorizationRef auth = NULL; 
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth);


AuthorizationCopyRights(auth, &setOfRights, kAuthorizationEmptyEnvironment,
                              (kAuthorizationFlagDefaults
                               | kAuthorizationFlagInteractionAllowed
                               | kAuthorizationFlagExtendRights), NULL);

建议您参考the docs了解详情。

答案 1 :(得分:1)

这对我有用:

NSString * appPath = [[NSBundle mainBundle] bundlePath];

// This will retrieve the path for the application
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath]; 

// Create a reference to the shared file list.
// We are adding it to the current user only.
// If we want to add it all users, use
// kLSSharedFileListGlobalLoginItems instead of
//kLSSharedFileListSessionLoginItems
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
    //Insert an item to the list.
    LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,kLSSharedFileListItemLast, NULL, NULL,url, NULL, NULL);
    if (item){
        CFRelease(item);
    }
}   

CFRelease(loginItems);

答案 2 :(得分:0)

NSString * appPath = [[NSBundle mainBundle] bundlePath];

        // This will retrieve the path for the application
        CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath]; 

        LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL);
        if (loginItems) {
            //Insert an item to the list.
            LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,kLSSharedFileListItemLast, NULL, NULL,url, NULL, NULL);
            if (item){
                CFRelease(item);
            }
        }   

        CFRelease(loginItems);

这段代码不起作用?我用kLSSharedFileListGlobalLoginItems替换了kLSSharedFileListSessionLoginItems