Mac OS X Cocoa: applicationDidBecomeActive not being fired
16:51 09 Mar 2026

I'm making an app purely from code (so this is not a duplicate of this because the solution to that was configuring a nib file from XCode) and I want to detect when my application has become the main window.

I'm creating it via this:

@interface MilskoCocoa : NSObject {
  NSApplication *application;
  MilskoCocoaWindow *window;
  NSRect rect;
  MilskoCocoaView *view;
  MwLL parent;
}
+ (MilskoCocoa *)newWithParent:(MwLL)parent
                             x:(int)x
                             y:(int)y
                         width:(int)width
                        height:(int)height;

@end

// ...

@implementation MilskoCocoa
+ (MilskoCocoa *)newWithParent:(MwLL)parent
                             x:(int)x
                             y:(int)y
                         width:(int)width
                        height:(int)height
                        handle:(MwLL)r {
  MilskoCocoa *c = [MilskoCocoa alloc];
  [c retain];

  c->application = [NSApplication sharedApplication];
  c->rect = rectFlip(NSMakeRect(x, y, width, height));

  if (parent == NULL) {
    c->window = [[MilskoCocoaWindow alloc]
        initWithContentRect:c->rect
                  styleMask:(NSTitledWindowMask |
                             NSTexturedBackgroundWindowMask |
                             NSClosableWindowMask | NSMiniaturizableWindowMask |
                             NSResizableWindowMask)
                    backing:NSBackingStoreBuffered
                      defer:NO];
  } else {
    double offset = 0;
    MilskoCocoaWindow *parentWindow = parent->cocoa.real->window;
    offset =
        [parentWindow frameRectForContentRect:[parentWindow frame]]
            .size.height -
        [parentWindow contentRectForFrameRect:[parentWindow frame]].size.height;

    c->rect.origin.x += [parentWindow frame].origin.x;
    c->rect.origin.y -= [parentWindow frame].origin.y - offset;
    c->rect.origin.y -= offset;

    c->window =
        [[MilskoCocoaWindow alloc] initWithContentRect:c->rect
                                             styleMask:NSBorderlessWindowMask
                                               backing:NSBackingStoreBuffered
                                                 defer:NO];
  }
  [c->window
      setDelegate:[[MilskoCocoaWindowDelegate alloc] initWithWin:c->window]];

  [c->window makeKeyAndOrderFront:c->application];
  [c->window retain];

  if (parent != NULL) {
    MilskoCocoa *p = parent->cocoa.real;
    double offset = 0;
    offset = [p->window frameRectForContentRect:[p->window frame]].size.height -
             [p->window contentRectForFrameRect:[p->window frame]].size.height;
    [p->window addChildWindow:c->window ordered:NSWindowAbove];
    [c->window setHasShadow:MwFALSE];
    [c->window setParentWindow:p->window];

    c->rect.origin.x += [p->window frame].origin.x;
    c->rect.origin.y -= [p->window frame].origin.y - offset;
    c->rect.origin.y -= offset;
  } else {
    [c->application setActivationPolicy:NSApplicationActivationPolicyRegular];
    [c->application activateIgnoringOtherApps:true];
    [c->window makeFirstResponder:c->view];
    [c->window makeKeyAndOrderFront:nil];
  }
  [c->application setDelegate:[[MilskoCocoaApplicationDelegate alloc]
                                  initWithAppl:c->application]];
  [c->application retain];

  c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect];
  [c->view retain];

  [c->window setContentView:c->view];

  c->parent = parent;

  [c->application finishLaunching];

  return c;
}

As shown in the code, I also have this application delegate:

@implementation MilskoCocoaApplicationDelegate
- (MilskoCocoaApplicationDelegate *)initWithAppl:(NSApplication *)_appl {
  self->appl = _appl;
  return self;
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
  printf("test\n");
}
@end

My understanding is applicationDidBecomeActive should get called when my application becomes the main window (when I click anywhere within it other then the title bar). This doesn't happen, though, the log I placed isn't ever hit. Is there a different function I should be calling?

It's worth mentioning that due to the design of the library I'm porting I'm not using any of the callbacks, instead my main loop involves manually calling nextEventMatchingMask, executing code based on the resulting NSEvent's type, and then doing [NSWindow sendEvent:{event}]. This has caused trouble in the past as the equivalent callback functions aren't fired either as a result of me doing this.

objective-c