如何让程序在登录时自动启动? [英] How can I make program automatically startup on login?

查看:192
本文介绍了如何让程序在登录时自动启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置菜单栏应用程序在登录时自动启动?我想这是默认。

How can I set a menu bar app to automatically startup on login? I want this to be default. Can i do this simply by adding a bool in the info.plist?

推荐答案

您可以使用会话登录项共享文件列表。这是在您在个人资料设置下检查登录项时显示在系统偏好设置中的列表。

You use the Session Login Items shared file list. This is the list that is displayed in System Preferences when you check your login items under your profile settings.

应用程序中的典型方案是在应用程序的偏好,允许用户选择他们想要启动应用程序或不登录。如果您打算通过应用商店分发,请勿将应用默认设置为在登录时启动。您将被拒绝:)

The typical scenario in an app is to provide a checkbox in the app's preferences, that allows the user to choose wether they want to start the app or not on login. If you intend to distribute through the app store DO NOT set the app to launch on login by default. You will get rejected :)

因此,在这种情况下,我们将在App Delegate中创建一个名为launchOnLogin的属性,复选框到此属性。

So, in this scenario, we will make a property, say in the App Delegate, called "launchOnLogin" and we will bind the value of the check box to this property.

getter方法将检查我们的应用程序的bundle ID是否在共享列表中并返回true或false。

The getter method will check if our app's bundle id is in the shared list and return true or false.

这里:

AppDelegate.h

AppDelegate.h

@property (atomic, assign) BOOL launchOnLogin;

AppDelegate.m

- (BOOL)launchOnLogin 
{
    LSSharedFileListRef loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    CFArrayRef snapshotRef = LSSharedFileListCopySnapshot(loginItemsListRef, NULL);
    NSArray* loginItems = [NSMakeCollectable(snapshotRef) autorelease];
    NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    for (id item in loginItems) {
        LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
        CFURLRef itemURLRef;
        if (LSSharedFileListItemResolve(itemRef, 0, &itemURLRef, NULL) == noErr) {
            NSURL *itemURL = (NSURL *)[NSMakeCollectable(itemURLRef) autorelease];
            if ([itemURL isEqual:bundleURL]) {
                return YES;
            }
        }
    }
    return NO;
}

- (void)setLaunchOnLogin:(BOOL)launchOnLogin
{    
    NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    LSSharedFileListRef loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);

    if (launchOnLogin) {
        NSDictionary *properties;
        properties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"com.apple.loginitem.HideOnLaunch"];
        LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsListRef, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)bundleURL, (CFDictionaryRef)properties,NULL);
        if (itemRef) {
            CFRelease(itemRef);
        }
    } else {
        LSSharedFileListRef loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
        CFArrayRef snapshotRef = LSSharedFileListCopySnapshot(loginItemsListRef, NULL);
        NSArray* loginItems = [NSMakeCollectable(snapshotRef) autorelease];

        for (id item in loginItems) {
            LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
            CFURLRef itemURLRef;            
            if (LSSharedFileListItemResolve(itemRef, 0, &itemURLRef, NULL) == noErr) {
                NSURL *itemURL = (NSURL *)[NSMakeCollectable(itemURLRef) autorelease];
                if ([itemURL isEqual:bundleURL]) {
                    LSSharedFileListItemRemove(loginItemsListRef, itemRef);
                }
            }
        }
    }
}


$ b b

这是很多。现在,如果您正确地执行绑定和一切,您将看到您的应用程序如何实时显示和从系统偏好设置列表中消失。

That's pretty much it. Now if you do the binding and everything correctly, you'll see how your app appears and dissappears from the System Preferences list in realtime.

// Get the path of the app
NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

// Get the list you want to add the path to
LSSharedFileListRef loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);

// Set the app to be hidden on launch
NSDictionary *properties = @{@"com.apple.loginitem.HideOnLaunch": @YES};

// Add the item to the list
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsListRef, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)bundleURL, (CFDictionaryRef)properties,NULL);

请注意,上面的代码是retain / release,但是将它转换为ARC你需要它。

Please note that the code above is retain/release but it is pretty trivial to convert it to ARC if you need it.

希望这有助。

这篇关于如何让程序在登录时自动启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆