Thursday, April 6, 2017

Programmatically Creating a Window on macOS

Programmatically creating windows without using a NIB file can be tricky on the Mac. Especially if you're developing in C or C++ instead of Objective-C. Here's one of the issue most common that may come up.

Assuming you got the window creation code right, you may be thinking everything is going to work, but you get this error:

Oct 30 17:34:39  myapp[48617] : kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Oct 30 17:34:39 myapp[48617] : kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.



This means your NSApplication is not being initialized. In other words, the Objective-C side of your system needs to be properly initialized. Adding the following line may help:

[NSApplication sharedApplication];

Most likely you aren't trying to do this so it is never an issue, but for those few souls out there this might just save you some serious time.

No comments:

Post a Comment