Search code examples
objective-cbackground-foreground

How to scan if Mac is in background / foreground


Is there any posible way to know if a Mac is in background or foreground?

In iOS we have the following methods: applicationWillEnterForeground applicationWillenterBackground

But is there any way to know if a Mac is active or inactive?


Solution

  • It depends on your application layout, but in general use an NSApplication delegate and implement:

    - (void)applicationWillBecomeActive:(NSNotification *)aNotification
    

    as well as its counterpart

    - (void)applicationWillResignActive:(NSNotification *)aNotification
    

    There are other notifications as well. See the linked document above. Also note that OS X allows real multitasking and thus background/foreground usually refer to having focus or not having focus.