Jak otworzyć aplikację przy logowaniu? [duplikat]

To pytanie ma już odpowiedź tutaj:

Tylko zastanawiam się, jak Mogę zrobić moja aplikacja otwiera się automatycznie przy logowaniu, ale sprawiają, że to być w stanie być włączane i wyłączane za pomocą pola wyboru w oknie preferencji.

 28
Author: Joshua, 2009-05-02

4 answers

Jest porządny opis co robić w CocoaDev.

Zasadniczo będziesz chciał użyć API w LaunchServices / LSSharedFileList.h jeśli możesz kierować Mac OS X 10.5 lub nowszy. Przed 10.5 nie było czystego API, więc trzeba ręcznie manipulować elementami logowania (przykładowy kod w Developer Connectiong ).

Oto przykładowy kod(dead) dla Leoparda wspomniałem w komentarzach. Znalezione przez ten wpis na blogu . The code you trzeba włączyć lub wyłączyć uruchamianie przy logowaniu jest w kontrolerze.m.

 19
Author: Naaff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-02 13:51:38

Oto kod, którego używam, jest oparty na źródle Growl.

+ (BOOL) willStartAtLogin:(NSURL *)itemURL
{
    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, /*outRef*/ NULL);
            if (err == noErr) {
                foundIt = CFEqual(URL, itemURL);
                CFRelease(URL);

                if (foundIt)
                    break;
            }
        }
        CFRelease(loginItems);
    }
    return (BOOL)foundIt;
}

+ (void) setStartAtLogin:(NSURL *)itemURL enabled:(BOOL)enabled
{
    OSStatus status;
    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, /*outRef*/ 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);
    }       
}

Jeśli chcesz mieć łatwe do zaimplementowania pole wyboru, stwórz @property BOOL startAtLogin; w jednej ze swoich klas i zaimplementuj je w następujący sposób. Wystarczy powiązać wartość pola wyboru z właściwością i wszystko powinno działać bezproblemowo.

- (NSURL *)appURL
{
    return [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
}

- (BOOL)startAtLogin
{
    return [LoginItem willStartAtLogin:[self appURL]];
}

- (void)setStartAtLogin:(BOOL)enabled
{
    [self willChangeValueForKey:@"startAtLogin"];
    [LoginItem setStartAtLogin:[self appURL] enabled:enabled];
    [self didChangeValueForKey:@"startAtLogin"];
}
 39
Author: Nick Moore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-23 12:22:23

Wywołaj metodę wklejoną poniżej z adresem URL pliku wskazującym na Twoją aplikację, aby dodać ją do pozycji logowania bieżącego użytkownika.

Aby wyłączyć ponownie, musisz pobrać ten sam loginListRef, przekonwertować go na tablicę i iterację, aż znajdziesz element z adresem url, który chcesz wyłączyć. Na koniec wywołaj LSSharedFileListItemRemove z odpowiednimi argumentami.

Powodzenia:)

- (void)enableLoginItemWithURL:(NSURL *)itemURL
{
    LSSharedFileListRef loginListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);

    if (loginListRef) {
        // Insert the item at the bottom of Login Items list.
        LSSharedFileListItemRef loginItemRef = LSSharedFileListInsertItemURL(loginListRef, 
                                             kLSSharedFileListItemLast, 
                                             NULL, 
                                             NULL,
                                             (CFURLRef)itemURL, 
                                             NULL, 
                                             NULL);     
        if (loginItemRef) {
            CFRelease(loginItemRef);
        }
        CFRelease(loginListRef);
    }
}
 12
Author: Dirk Stoop,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-02 16:36:01
 3
Author: Nathan Kinsinger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:28:48