老谭笔记

注册程序为开机自启动

让一个app软件注册为开机自启动:

1
2
3
4
5
6
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
//url为app所在的目录
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath];
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, NULL, NULL, url, NULL, NULL);
CFRelease(item);
CFRelease(loginItems);

取消app开机启动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
UInt32 seedValue;
CFURLRef thePath;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
for (id item in (NSArray *)loginItemsArray) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
//appPath目录为要取消开机启动app的路径
if ([[(NSURL *)thePath path] hasPrefix:appPath]) {
LSSharedFileListItemRemove(theLoginItemsRefs, itemRef); // Deleting the item
}
CFRelease(thePath);
}
}
CFRelease(loginItemsArray);
CFRelease(loginItems);