API to detect active session in Mac OS X with fast user switch

后端 未结 3 1011
生来不讨喜
生来不讨喜 2021-01-18 03:07

On Mac OS X Snow Leopard with fast user switching enabled, is there an API to detect whether or not my application is running in the active user session? I.e. the session cu

3条回答
  •  野的像风
    2021-01-18 03:41

    I avoided using User Switch Notifications and instead have found two other possibilities:

    1. Use CGMainDisplayID() from Core Graphics. Store the main display ID when your app first starts and keep polling it. It will change to a different display ID when switching to another user. Problem with this is it may also change for other reasons e.g. changing which display is the primary display in a multi-screen setup.

    2. Use CGSessionCopyCurrentDictionary() also from Core Graphics, and retrieve the kCGSessionOnConsoleKey Boolean value from the dictionary. This indicates whether your user session is attached to console.

    Both of these require polling but this is fine for my purposes. User Switch Notifications would be a better choice if you need to be event-driven.

提交回复
热议问题