知道用户是否启动了应用 [英] Know if the user launched an app

查看:111
本文介绍了知道用户是否启动了应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个标题可能看起来很奇怪,但请耐心等待.我有一个可以由用户根据喜好设置的应用程序,以便在登录时启动.这意味着我可以预期有时该应用程序将由用户启动(单击Dock/Finder等),而另一些时候,该应用程序将在登录时由系统自动启动.

Alright, this title might seem strange, but bear with me. I have an app which can be set on its preferences by the user to launch at login. That means I can expect sometimes the app will be launched by the user (clicking on the Dock/Finder, etc), but some other times the app will be launched automatically by the system, on login.

我想在用户启动应用程序时显示一个窗口,而不是在自动启动应用程序时显示一个窗口(因为我认为这会给用户带来痛苦).我该怎么办?

I would like to show a window when the app is launched by the user, but not when it is launched automatically (as I imagine that would be a pain for the user). How can I do that?

推荐答案

尽管可能取决于您打算如何自动启动应用程序,但是您可以使用命令行参数来区分系统启动与用户启动.

Although it may depend on how you intend to automate the launch of the app, you could use command line arguments to distinguish between system launch vs. user launch.

因此,命令行启动可能像这样:

So, the command line launch might like like this:

MyApp -autoLaunch "Y"

要在Cocoa应用程序中解析命令行参数,可以使用NSUserDefaults(是,您可以!):

To parse the command line args in a Cocoa app, you could use NSUserDefaults (Yes, you can!):

if( ![[NSUserDefaults standardUserDefaults] objectForKey:@"autoLaunch"] isEqualToString:"Y"] ) {
    // do something for user initiated launch
}

这篇关于知道用户是否启动了应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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