Mac OSX:如何知道应用是否在会话启动/登录时自动启动? [英] Mac OSX: how to know if app automatically launched at session startup/login?

查看:184
本文介绍了Mac OSX:如何知道应用是否在会话启动/登录时自动启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的用户可以选择是否要在会话启动时启动我的应用程序.

The users of my app can choose if they want (or not) launch my app at their session startup.

为此,我使用LSSharedFileListRef,如此处所述:您如何在登录时打开应用?

To do this, I use LSSharedFileListRef as described here : How do you make your App open at login?

我现在想知道是在会话启动时还是通过单击扩展坞中的图标自动启动了我的应用程序.确实,在这两种情况下,我必须采取不同的动作.

What I want now is to know if my app has been launched automatically at session startup or via a click on the icon in the dock. Indeed, I have to do different actions in these two cases.

我感觉无法在以下委托方法中使用通知的参数来做到这一点:

I have got the feeling that it is not possible to use the parameters of the notification in the following delegate method to do this:

- (void)applicationDidFinishLaunching:(NSNotification *)notification

我看过以下帖子,但它们无济于事:

I have seen the following posts but they do not help:

我如何知道如何我的应用程序是在Mac OS上启动的? =>没有答案,除了一些其他文章的链接无济于事...

How can I know how my app was launched on Mac OS? => NO ANSWER except some links to other posts which do not help more...

知道用户是否启动了应用程序 =>我不知道看不到如何设置/获取本文定义的"Y"参数

Know if the user launched an app => I don't see how to set/get the "Y" parameter defined in this post

Mac OS X:打开登录时应用程序不显示主窗口 =>处理启动时是否隐藏主窗口的事实;我想要的是更笼统的:如何知道应用程序是如何启动的?

Mac OS X: open application at login, without showing the main window => deals with the fact to hide or not the main window at startup ; what I want is more general: how to know how the app has been launched?

有人可以帮助我吗?

谢谢!

推荐答案

我要检查父进程ID".如果等于1,则表示它是在启动时通过启动而启动的.

I'd check Parent Process ID. If it equals 1 it means it was launched by launchd at start up time.

struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
    return OPProcessValueUnknown;
if (length == 0)
    return OPProcessValueUnknown;

 //info.kp_eproc.e_ppid is what you need 

这篇关于Mac OSX:如何知道应用是否在会话启动/登录时自动启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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