Search code examples
objective-cxcode4

I want a code that will be called everytime an app is launched, activated, or reactivated


- (void)applicationWillBecomeActive:(NSNotification *)notification
{
    NSRunAlertPanel(@"Wee I am called", @"Hello", @"OK", @"Alternate Button", @"OtherButton");


    [showdesktop showDeskHideMe];
}

The problem with code above is it's only launched when the app is first launched.

If after that I open up other apps, and then click the apps again from the dock, the code will not be launched.

How do I do that?

How to "deactivate" an application anyway?

Note: This is about MacOs, not IOS!!!


Solution

  • Have a look at the "Notifications" section here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsapplication_Class/Reference/Reference.htm ( Sorry, can't find a direct link )

    There are a bunch of notifications which you can catch ranging a wide array of different events. For example, NSApplicationDidUnhideNotification seems interesting in your case. You can use the NSNotificationCenter to pick up on these notifications. For more information about the NSNotificationCenter check: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html#//apple_ref/doc/uid/10000043i