I have a LaunchAgent application, which is launched when ever the user logs in to the system.
The LaunchAgent file is in /Library/LaunchAgents so the app launches for all users who log in to the sustem.
This is what I want actually.
But the problem is, when ever I use fast user switch and login to another user, the activity monitor shows that the application is running for both the users. What I want is that only the instance of current user should exist all the time. And ever I switch user the previous instance should exit.
Please help me on this. Thanks
You can listen for user switches using either Carbon or Cocoa -- see User Switch Notifications in the Multiple User Environments programming guide. However, if you exit your process when you get a user switch notification, launchd won't re-launch it for you when the system is switched back.
The cleanest way to handle this would be to make sure your app can closes any resources that need to be exclusively used by a user when the system switches away from a user's login session, and re-open them when you're notified that the system is returning to that session.
If that's not possible, you could break your app into two processes; a faceless helper that is launched by launchd, which never exits and whose only job is to run your full app, and the full app itself. When the system switches away from the current user, your full app can exit, but the helper can stay running; when switching back, the helper can re-launch your full app.
Lastly, having launchd take action when the current login session is switched sounds like useful functionality -- consider filing a feature request on Apple's bug tracker.