有没有一种方法来区分由登录或由用户启动服务启动的应用程序? [英] Is there a way to distinguish app being started by Launch Services at login or by user?

查看:144
本文介绍了有没有一种方法来区分由登录或由用户启动服务启动的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cocoa应用程序可以将自己添加到 LSSharedFileList 的登录项列表中。这将允许应用程序在用户登录时启动。但是,有没有方法可以告诉用户是启动应用程序还是应用程序在登录时自动启动?这是有用的,因为在一个情况下,我们可以显示一个用户界面,在另一个我们可以隐藏UI,并在后台运行应用程序作为菜单应用程序。

Cocoa app can add themselves to LSSharedFileList's list of login items. This will allow application to be started when user logs in. However, is there a way to tell whether user started the application or the app auto-started at login? This is useful because in one case we can show a user interface in another we can hide the UI and run the app in background as a menubar app.

推荐答案

这里有一些代码。我不知道它返回的登录项目,但如果你尝试它,并评论,我会更新的帖子。它会返回 com.apple.Finder for Finder和 com.apple.dt.Xcode for Xcode。

Here's some code for this. I'm not sure what it returns for login items, but if you try it out and comment I'll update the post. It does return com.apple.Finder for Finder and com.apple.dt.Xcode for Xcode.

+ (NSString *) bundleIdentifierOfParentProcess {
    NSString *result = nil;
    ProcessSerialNumber psn = {0, 0};
    if (0 == GetCurrentProcess(&psn)) {
        ProcessInfoRec myProcessInfo;
        myProcessInfo.processInfoLength = sizeof(ProcessInfoRec);
        myProcessInfo.processName = NULL;
        myProcessInfo.processAppRef = NULL;
        if (0 == GetProcessInformation(&psn, &myProcessInfo)) {
            ProcessSerialNumber parentPSN = myProcessInfo.processLauncher;
            CFDictionaryRef parentProcessInfo =
            ProcessInformationCopyDictionary(&parentPSN,
                                             kProcessDictionaryIncludeAllInformationMask);
            if (parentProcessInfo) {
                result =
                [(__bridge NSDictionary *) parentProcessInfo objectForKey:
                (__bridge id) kCFBundleIdentifierKey];
                CFRelease(parentProcessInfo);
            }
        }
    }
    return result;
}

parentProcessInfo 字典中的值也可能有帮助,如果包标识符不够有意义。

parentProcessInfo is a dictionary full of values which may also be helpful, in case the bundle identifier isn't meaningful enough.

这篇关于有没有一种方法来区分由登录或由用户启动服务启动的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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