Cocoa应用程序如何将自身添加为全局登录项目? [英] How can a Cocoa application add itself as a global login item?

查看:202
本文介绍了Cocoa应用程序如何将自身添加为全局登录项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了

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。还有别的东西需要做吗?某些类型的授权?

LSSharedFileListInsertItemURL() just returned NULL when I built and ran the application. Is there something else that I need to do? Some kind of authorization?

注意:这里的用例是针对全局登录项目,即使用kLSSharedFileListGlobalLoginItems而不是kLSSharedFileListSessionLoginItems。 p>

NOTE: The use-case here is for global login items, that is using kLSSharedFileListGlobalLoginItems and not kLSSharedFileListSessionLoginItems.

推荐答案

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

I got this working. All I had to do was add these lines before I insert the app into the login items:

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

LSSharedFileListSetAuthorization 的文档说为此得到正确的 system.global-login-items ,但它工作了!

The docs for LSSharedFileListSetAuthorization say that we have to get the right system.global-login-items for this, but it worked nevertheless!

如果用户不是管理员,则失败。对于它也工作,你必须这样做:

But this will fail if the user is not an administrator. For it to work then too, you'll have to do this:

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);

建议您参考文档

这篇关于Cocoa应用程序如何将自身添加为全局登录项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆