LSSharedfileListRef globalLoginItems = LSSharedfileListCreate(NulL,kLSSharedfileListGlobalLoginItems,NulL);if (globalLoginItems) { LSSharedfileListItemRef ourLoginItem = LSSharedfileListInsertItemURL(globalLoginItems,kLSSharedfileListItemLast,NulL,(CFURLRef)[[NSBundle mainBundle] bundleURL],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}};AuthorizationRights setofRights = {1,right};AuthorizationRef auth = NulL; AuthorizationCreate(NulL,&auth);AuthorizationcopyRights(auth,&setofRights,(kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights),NulL);
有关详细信息,建议参考the docs.
总结以上是内存溢出为你收集整理的Cocoa应用程序如何将自身添加为全局登录项?全部内容,希望文章能够帮你解决Cocoa应用程序如何将自身添加为全局登录项?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)