Skip to content

Instantly share code, notes, and snippets.

@matiaz
Last active December 28, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiaz/7436054 to your computer and use it in GitHub Desktop.
Save matiaz/7436054 to your computer and use it in GitHub Desktop.
#dev #mac #xcode -Add App to start /load / open on login -Agregar el App a que se inicie / cargue / abra después de iniciar la sesión
-(void) deleteAppFromLoginItem{
NSString * appPath = [[NSBundle mainBundle] bundlePath];
// This will retrieve the path for the application
// For example, /Applications/test.app
CFURLRef url = (__bridge 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 = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);
int i = 0;
while (i != [loginItemsArray count]) {
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)[loginItemsArray
objectAtIndex:i];
//Resolve the item with URL
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
NSString * urlPath = [(__bridge NSURL*)url path];
if ([urlPath compare:appPath] == NSOrderedSame){
LSSharedFileListItemRemove(loginItems,itemRef);
}
}
i++;
}
}
}
-(void) addAppAsLoginItem{
NSString * appPath = [[NSBundle mainBundle] bundlePath];
// This will retrieve the path for the application
// For example, /Applications/test.app
CFURLRef url = (__bridge 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment