2011-03-27 70 views
5

我試圖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,當我建立並運行應用程序。還有什麼我需要做的嗎?某種授權?

注意:這裏的用例是全局登錄項目,即使用kLSSharedFileListGlobalLoginItems而不是kLSSharedFileListSessionLoginItems。

回答

5

我得到了這個工作。我所要做的就是之前我插入應用程序添加幾行代碼寫進登錄項:

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

LSSharedFileListSetAuthorization文檔說,我們必須找到正確的system.global-login-items這一點,但它仍然工作!

但是,如果用戶不是管理員,則會失敗。對於它來工作,那麼也一樣,你就必須做到這一點:

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

這也是可取的參考the docs瞭解詳情。

1

這個工作對我來說:

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

謝謝,但kLSSharedFileListSessionLoginItems也適用於我。我希望這爲所有的用戶工作。使用kLSSharedFileListGlobalLoginItems,即。 – Plumenator 2011-03-27 12:37:02

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

不行的代碼?我用kLSSharedFileListGlobalLoginItems替換kLSSharedFileListSessionLoginItems

+0

沒錯。我把項目作爲NULL。 – Plumenator 2011-03-27 12:50:57

相關問題