Guest User

Untitled

a guest
Jul 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. -(void) addAppAsLoginItem {
  2. if ([self wasAppAddedAsLoginItem]) return;
  3.  
  4. NSString * appPath = [[NSBundle mainBundle] bundlePath];
  5.  
  6. // This will retrieve the path for the application
  7. // For example, /Applications/test.app
  8. CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
  9.  
  10. // Create a reference to the shared file list.
  11. // We are adding it to the current user only.
  12. // If we want to add it all users, use
  13. // kLSSharedFileListGlobalLoginItems instead of
  14. //kLSSharedFileListSessionLoginItems
  15. LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  16. if (loginItems) {
  17. //Insert an item to the list.
  18. LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,
  19. kLSSharedFileListItemLast, NULL, NULL,
  20. url, NULL, NULL);
  21. if (item){
  22. CFRelease(item);
  23. }
  24. CFRelease(loginItems);
  25. }
  26. }
  27.  
  28. - (BOOL)wasAppAddedAsLoginItem {
  29. NSString * appPath = [[NSBundle mainBundle] bundlePath];
  30. CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
  31. LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  32.  
  33. BOOL ret = NO;
  34.  
  35. if (loginItems) {
  36. UInt32 seedValue;
  37. //Retrieve the list of Login Items and cast them to
  38. // a NSArray so that it will be easier to iterate.
  39.  
  40. CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
  41.  
  42. for (id item in (__bridge NSArray *)loginItemsArray) {
  43. LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
  44. //Resolve the item with URL
  45. if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
  46. NSString * urlPath = [(__bridge NSURL*)url path];
  47. if ([urlPath compare:appPath] == NSOrderedSame){
  48. ret = YES;
  49. }
  50. }
  51. }
  52. CFRelease(loginItemsArray);
  53. CFRelease(loginItems);
  54. }
  55.  
  56.  
  57. return ret;
  58. }
  59.  
  60. -(void) deleteAppFromLoginItem {
  61. NSString * appPath = [[NSBundle mainBundle] bundlePath];
  62. CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
  63. LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  64.  
  65. if (loginItems) {
  66. UInt32 seedValue;
  67. CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
  68.  
  69. for (id item in (__bridge NSArray *)loginItemsArray) {
  70. LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
  71. if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
  72. NSString * urlPath = [(__bridge NSURL*)url path];
  73. if ([urlPath compare:appPath] == NSOrderedSame){
  74. LSSharedFileListItemRemove(loginItems, itemRef);
  75. }
  76. }
  77. }
  78. CFRelease(loginItemsArray);
  79. CFRelease(loginItems);
  80. }
  81. }
Add Comment
Please, Sign In to add comment