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

2024-02-24

I tried

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。


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

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 http://developer.apple.com/library/mac/#documentation/Security/Conceptual/authorization_concepts/01introduction/introduction.html了解详情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cocoa 应用程序如何将自身添加为全局登录项? 的相关文章

随机推荐