Ideal way to single-instance apps on the Mac

后端 未结 5 1671
野趣味
野趣味 2020-12-06 03:18

On Windows, it\'s common practice to create a named mutex and use the presence of that to determine that an instance of a given app is already running. This has its drawback

相关标签:
5条回答
  • 2020-12-06 03:23

    Macs do not have instances in the same way as Windows does. Generally speaking you want the application running twice you physically need to copy the binary and then double click on the copied version as well.

    If you need two instances of an application running then you are not thinking like a Mac user :).

    Edit: This is technically not true. Check the comments.

    0 讨论(0)
  • 2020-12-06 03:25

    Mapping process management among disparate operating systems doesn't work. Or doesn't work well. By default and without particular effort, you get one copy and only one copy of the application.

    Here's a previous similar question that goes a step further than this current question, and with some replies that discuss interlocking when there are multiple copies of an image, or multiple applications that need coordination.

    How to detect whether an OS X application is already launched

    For an introduction to the run-time context and particularly around Mac OS X daemons and agents (and for those cases when you do need to have multiple copies of an executable running, as a pool or such and akin to Apache), see:

    http://developer.apple.com/technotes/tn2005/tn2083.html

    0 讨论(0)
  • 2020-12-06 03:33

    If you're writing a Cocoa application, you can use NSWorkspace to see if another process is running with your bundle identifier. I've seen a few apps that present a dialog and say: "An instance of this app is already running" - I think Firefox does it, actually.

    It's not a very "mac-ish" approach, but it will get the job done.

    0 讨论(0)
  • 2020-12-06 03:40

    To elaborate further on using NSWorkspace. Try using launchedApplications in NSWorkspace. This returns an NSArray containing a dictionary for each launched application. You can loop through the array to see if the app you are looking for is already running. I would advise that you use the value with the key NSApplicationBundleIdentifier which will have a value like "com.mycompany.myapp" rather than looking for the name. If you need to find the bundle identifier for an app you can look at its info.plist file in the app package.

    0 讨论(0)
  • 2020-12-06 03:48

    If you were to deploy your application with Java Web Start (JWS), you could use javax.jnlp.SingleInstanceService. JWS provisioning would also provide automatic program updates.

    0 讨论(0)
提交回复
热议问题