Creating and/or Removing a Login Item
来源:互联网 发布:linux怎么防御勒索 编辑:程序博客网 时间:2024/05/21 11:00
转自: http://cocoatutorial.grapewave.com/2010/02/creating-andor-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.
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.
The following method will retrieve the list of existing LoginItems
extern LSSharedFileListRef LSSharedFileListCreate( CFAllocatorRef inAllocator, CFStringRef inListType, CFTypeRef listOptions)
Parameters:
- inAllocator - CFAllocatorRef used to allocate the LSSharedFileListRef . NULL means default allocator.
- inListType - A constant indicating list type to create (in our case, it will be kLSSharedFileListSessionLoginItems or
kLSSharedFileListGlobalLoginItems). - 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
extern LSSharedFileListItemRef LSSharedFileListInsertItemURL( LSSharedFileListRef inList, LSSharedFileListItemRef insertAfterThisItem, CFStringRef inDisplayName, IconRef inIconRef, CFURLRef inURL, CFDictionaryRef inPropertiesToSet, CFArrayRef inPropertiesToClear)
Parameters:
- inList – The list to which we want to add a new Login Item.
- 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. - inDisplayName – Display name of the new item. Can be NULL. If NULL, it will use the application’s name.
- inIconRef – Icon of the new item. Can be NULL. If NULL, it will use the default application icon.
- 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.
extern OSStatus LSSharedFileListItemRemove( LSSharedFileListRef inList, LSSharedFileListItemRef inItem)
Parameters:
- inList – The list from which the item will be removed.
- 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.
- inItem – The item we are trying to resolve.
- inFlags – This is generally NULL.
- outURL – The URL to which we are trying to resolve the item to.
- 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.appCFURLRef 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 //kLSSharedFileListSessionLoginItemsLSSharedFileListRef 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.appCFURLRef 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 URLif (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.
- Creating and/or Removing a Login Item
- Creating and/or Removing a Login Item(添加/移除登陆项)
- Setting or removing an image for an CListCtrl item
- Login shell or a non-login shell?
- Creating a Remember Me Login @ JDJ
- [matlab]Removing a block and making connections
- Chapter 22: Using Item Renderers and Item Editors--Creating an item renderer and item editor
- Chapter 22: Using Item Renderers and Item Editors--Creating drop-in item renderers and item editors
- Chapter 22: Using Item Renderers and Item Editors--Creating item renderers and item editor components
- Creating a Custom Login Page for SharePoint 2010
- Removing the trailing file name and backslash from a path
- Creating a User in a Domain and Creating Organizational Units
- Chapter 22: Using Item Renderers and Item Editors--Creating inline item renderers and editors
- Creating a List Item instance programmatically using the object model
- Custom redirect after creating a new Sharepoint Item
- Creating and Managing a Database with DBCA
- creating and drawing on a new UIImage
- Tutorial 2: Creating a Framework and Window
- TCPDUMP详解
- 变量为什么要分开在头部和is部分两个地方声明呢?
- 关于类和对象的实践笔记——类的属性和静态属性
- 基本命令随记
- 腾讯 vs 百度
- Creating and/or Removing a Login Item
- 单片机秒表计时器
- Nginx应用案例分享:压力测试
- 当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“D:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NE
- hibernate使用SessionFactory的时候用单例和JUnit里的创建方法
- OOA、OOD、OOP
- Java Socket网络编程初级入门
- zf 视图 $this->view
- 关于solr不能搜索中文