Skip to content

Instantly share code, notes, and snippets.

@iolate
Last active July 8, 2019 15:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iolate/7963775 to your computer and use it in GitHub Desktop.
Save iolate/7963775 to your computer and use it in GitHub Desktop.
-(void) addAppAsLoginItem {
if ([self wasAppAddedAsLoginItem]) return;
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);
}
}
- (BOOL)wasAppAddedAsLoginItem {
NSString * appPath = [[NSBundle mainBundle] bundlePath];
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
BOOL ret = NO;
if (loginItems) {
UInt32 seedValue;
//Retrieve the list of Login Items and cast them to
// a NSArray so that it will be easier to iterate.
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
for (id item in (__bridge NSArray *)loginItemsArray) {
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
//Resolve the item with URL
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
NSString * urlPath = [(__bridge NSURL*)url path];
if ([urlPath compare:appPath] == NSOrderedSame){
ret = YES;
}
}
}
CFRelease(loginItemsArray);
CFRelease(loginItems);
}
return ret;
}
-(void) deleteAppFromLoginItem {
NSString * appPath = [[NSBundle mainBundle] bundlePath];
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seedValue;
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
for (id item in (__bridge NSArray *)loginItemsArray) {
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
NSString * urlPath = [(__bridge NSURL*)url path];
if ([urlPath compare:appPath] == NSOrderedSame){
LSSharedFileListItemRemove(loginItems, itemRef);
}
}
}
CFRelease(loginItemsArray);
CFRelease(loginItems);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment