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

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

I tried

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


        
3条回答
  •  天命终不由人
    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

提交回复
热议问题