Last active
December 28, 2015 03:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(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