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

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

I tried

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


        
3条回答
  •  甜味超标
    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);
    

提交回复
热议问题