How can a Cocoa application add itself as a global login item?

前端 未结 3 2021
-上瘾入骨i
-上瘾入骨i 2021-02-06 14:12

I tried

LSSharedFileListRef globalLoginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL);
if (globalLoginItems) {
    LSSharedFileLi         


        
相关标签:
3条回答
  • 2021-02-06 14:52

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

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

    It's also advisable to refer to the docs for details.

    0 讨论(0)
  • 2021-02-06 14:56
    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);
    

    Doesn't work this code? I replaced kLSSharedFileListSessionLoginItems with kLSSharedFileListGlobalLoginItems

    0 讨论(0)
  • 2021-02-06 15:15

    This works for me:

    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);
    
    0 讨论(0)
提交回复
热议问题