Creating and/or Removing a Login Item(添加/移除登陸項)

原文地址:http://cocoatutorial.grapewave.com/2010/02/creating-andor-removing-a-login-item/


Creating and/or Removing a Login Item

(創建或者移除登陸項)


Hi all,

Today, we will look at how to programmatically add an application to the Login Items. We will also look at removing it from the Login Items.


(如何以編程的方式添加應用程序到登錄項,以及移除登錄項)


Pre-requirements

We are going to be using the LaunchServices/LSSharedFileList.h to modify the list of login items. This is only available in OS X 10.5 and above. To support systems less than 10.5, you would have to use Apple Events. This will not be covered in this post.


(將會使用 LaunchServices/LSSharedFileList.h來修改登錄項列表,只適用於10.5及以上的系統,如果要支持10.5以前的,就需要用到Apple Events,此文不包括)


Methods

There are basically four methods we will have to use to add/remove a LoginItem. One will retrieve the list of LoginItems, the second will add a new item to that list, the third will remove an item from the list and that one is used to resolve the LoginItem with an URL.


(有四種方法來添加或者移除登錄項

1:獲取登錄項列表

2:添加一個登錄項

3:從登錄列表中移除一項

4:添加一個指向URL的登錄項)


The following method will retrieve the list of existing LoginItems


(1:獲取登錄列表)

extern LSSharedFileListRef

  LSSharedFileListCreate(

  CFAllocatorRef   inAllocator,

  CFStringRef      inListType,

  CFTypeRef        listOptions)


(參數的解釋就不介紹了)

Parameters:

  1. inAllocator - CFAllocatorRef used to allocate the LSSharedFileListRef . NULL means default allocator.
  2. inListType - A constant indicating list type to create (in our case, it will be kLSSharedFileListSessionLoginItems or
    kLSSharedFileListGlobalLoginItems).
  3. listOptions – Other options. This is generally NULL.

The second that we are going to look at will insert a new Login Item into the list, if it already does not exist. If it already exists, it will just move the item to the particular location in the list specified by the insertAfterThisItem parameter


(2:插入一個登錄項,如果該項已經存在,那它將會移動insertAfterThisItem這個列表中)

extern LSSharedFileListItemRef

  LSSharedFileListInsertItemURL(

  LSSharedFileListRef       inList,

  LSSharedFileListItemRef   insertAfterThisItem,

  CFStringRef               inDisplayName,

  IconRef                   inIconRef,

  CFURLRef                  inURL,

  CFDictionaryRef           inPropertiesToSet,

  CFArrayRef                inPropertiesToClear)

Parameters:

  1. inList – The list to which we want to add a new Login Item.
  2. insertAfterThisItem - Item after which new item has to be inserted. To insert at the beginning of
    the list use kLSSharedFileListItemBeforeFirst or to insert at the end of the list use kLSSharedFileListItemLast.
  3. inDisplayName – Display name of the new item. Can be NULL. If NULL, it will use the application’s name.
  4. inIconRef – Icon of the new item. Can be NULL. If NULL, it will use the default application icon.
  5. inURL – URL of the new item. This will be the full path where the .app is found(includes dummy.app).

The other two parameters are not that important at this point of time and they can be NULL.


The third method will allow the application to be removed from the list of Login Items.


(3:移除登錄項從登錄列表中)

extern OSStatus

  LSSharedFileListItemRemove(

  LSSharedFileListRef       inList,

  LSSharedFileListItemRef   inItem)

Parameters:

  1. inList – The list from which the item will be removed.
  2. inItem – The item to be removed


The last method is used to resolve the LoginItem with an URL when we are searching through the list of Login Items.

(4:根據URL獲取登錄項)

extern OSStatus

LSSharedFileListItemResolve(

LSSharedFileListItemRef   inItem,

UInt32                    inFlags,

CFURLRef *                outURL,

FSRef *                   outRef)


Parameters:

  1. inItem – The item we are trying to resolve.
  2. inFlags – This is generally NULL.
  3. outURL – The URL to which we are trying to resolve the item to.
  4. outRef – FSRef of original item. Can be NULL.

Implementation

For this example, I have created two methods. The first method will add your application to the login item and the second will remove the application from the list of Login Items.


(代碼就不用說了吧,都能看得懂)


-(void) addAppAsLoginItem{

NSString * appPath = [[NSBundle mainBundle] bundlePath];


// This will retrieve the path for the application

// For example, /Applications/test.app

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

}


-(void) deleteAppFromLoginItem{

NSString * appPath = [[NSBundle mainBundle] bundlePath];


// This will retrieve the path for the application

// For example, /Applications/test.app

CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath]; 


// Create a reference to the shared file list.

LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL,

                                               kLSSharedFileListSessionLoginItems, NULL);


if (loginItems) {

UInt32 seedValue;

//Retrieve the list of Login Items and cast them to

// a NSArray so that it will be easier to iterate.

NSArray  *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);

int i = 0;

for(i ; i< [loginItemsArray count]; i++){

LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)[loginItemsArray

                                                                               objectAtIndex:i];

//Resolve the item with URL

if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {

NSString * urlPath = [(NSURL*)url path];

if ([urlPath compare:appPath] == NSOrderedSame){

LSSharedFileListItemRemove(loginItems,itemRef);

}

}

}

[loginItemsArray release];

}

}


That is it. You can basically have a preference that asks the user whether he wants the app to open at login. Depending on the answer, you can invoke one of the method.

Good Luck.

(以上就搞定,你可以詢問用戶是否想要設置當前應用爲登錄項,然後根據用戶的選擇來定)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章