我只想问你是否在这里看到一些泄漏:
+ (BOOL)runAtStartupEnabled
{
NSURL *itemURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Boolean foundIt=false;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems)
{
UInt32 seed = 0U;
NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
for (id itemObject in currentLoginItems)
{
LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, NULL);
if (err == noErr)
{
foundIt = CFEqual(URL, itemURL);
CFRelease(URL);
if (foundIt)
{
break;
}
}
}
CFRelease(loginItems);
}
return (BOOL)foundIt;
}
+ (void)setRunAtStartupEnabled:(BOOL)enabled
{
NSURL *itemURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
LSSharedFileListItemRef existingItem = NULL;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if(loginItems)
{
UInt32 seed = 0U;
NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
for (id itemObject in currentLoginItems)
{
LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, NULL);
if (err == noErr)
{
Boolean foundIt = CFEqual(URL, itemURL);
CFRelease(URL);
if (foundIt)
{
existingItem = item;
break;
}
}
}
if (enabled && (existingItem == NULL))
{
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, (CFURLRef)itemURL, NULL, NULL);
}
else if(!enabled && (existingItem != NULL))
{
LSSharedFileListItemRemove(loginItems, existingItem);
}
CFRelease(loginItems);
}
}