Know if the user launched an app

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 13:26:11

问题


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?


回答1:


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"

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
}



回答2:


I don't have an exact answer to your question. However, may I suggest that your app should show the window if the window was visible when the user last quit your app?

This may be more in-line with the Mac UI guidelines' suggestion on restoring apps and windows, and is within the user's expectations.

Also, a user who set your app to launch at login will probably understand to close the window and not have it restored the next time, or make the system also hide your app during login.



来源:https://stackoverflow.com/questions/12465100/know-if-the-user-launched-an-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!