-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLaunchService.m
More file actions
110 lines (87 loc) · 3.1 KB
/
LaunchService.m
File metadata and controls
110 lines (87 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#import "LaunchService.h"
// Based on code from:
// http://bdunagan.com/2010/09/25/cocoa-tip-enabling-launch-on-startup/
// https://github.com/Mozketo/LaunchAtLoginController/blob/master/LaunchAtLoginController.m
@implementation LaunchService
+ (LaunchService *)sharedService {
static LaunchService *sharedService = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedService = [[self alloc] init];
});
return sharedService;
}
- (id)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
}
- (void)launchAtStartup:(BOOL)enabled {
LSSharedFileListRef listRef = LSSharedFileListCreate(NULL,
kLSSharedFileListSessionLoginItems,
NULL);
if (!listRef) {
return;
}
LSSharedFileListItemRef itemRef;
if (enabled) {
itemRef = LSSharedFileListInsertItemURL(listRef,
kLSSharedFileListItemLast,
NULL,
NULL,
(__bridge CFURLRef)[self appURL],
NULL,
NULL);
} else {
itemRef = [self findLoginItem];
if (itemRef) {
LSSharedFileListItemRemove(listRef, itemRef);
}
}
if (itemRef) {
CFRelease(itemRef);
}
CFRelease(listRef);
}
- (BOOL)isLaunchedAtStartup {
LSSharedFileListItemRef itemRef = [self findLoginItem];
BOOL isInList = itemRef != nil;
if (itemRef) {
CFRelease(itemRef);
}
return isInList;
}
- (LSSharedFileListItemRef)findLoginItem {
CFURLRef appURLRef = (__bridge CFURLRef)[self appURL];
LSSharedFileListRef listRef = LSSharedFileListCreate(NULL,
kLSSharedFileListSessionLoginItems,
NULL);
if (!listRef) {
return nil;
}
NSArray *loginItems = (__bridge NSArray *)LSSharedFileListCopySnapshot(listRef, nil);
for (int i = 0; i < [loginItems count]; i++) {
LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)loginItems[i];
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef currentItemURLRef = NULL;
LSSharedFileListItemResolve(currentItemRef, resolutionFlags, ¤tItemURLRef, NULL);
if (currentItemURLRef && CFEqual(currentItemURLRef, appURLRef)) {
CFRelease(currentItemURLRef);
CFRelease(listRef);
return currentItemRef;
}
if (currentItemURLRef) {
CFRelease(currentItemURLRef);
}
}
CFRelease(listRef);
return nil;
}
- (NSURL *)appURL {
return [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
}
@end