Skip to content

Instantly share code, notes, and snippets.

@graouts
Created September 28, 2012 09:16
Show Gist options
  • Save graouts/3798828 to your computer and use it in GitHub Desktop.
Save graouts/3798828 to your computer and use it in GitHub Desktop.
Launching the iOS Simulator and receiving a notification when it's ready (Node.js)
var $ = require('NodObjC');
$.framework('Foundation');
// get the distributed notification center
var notificationCenter = $.NSDistributedNotificationCenter('defaultCenter');
// this is the observer and the single method we register as the notification
var observer = {
receivedNotification : function (notification) {
console.log('received notification', notification);
}
};
// create our selector
var selector = $.NSSelectorFromString($('receivedNotification:'));
// and the name of the notification we're interested in
var name = $('com.apple.iphonesimulator.ready');
// add the observer
notificationCenter('addObserver', observer, 'selector', selector, 'name', name, 'object', null);
// launch the simulator
require('child_process').exec("open -a 'iPhone Simulator'");
@graouts
Copy link
Author

graouts commented Sep 28, 2012

Running this code results in the following error:

Assertion failed: (!handle.IsEmpty()), function Unwrap, file /Users/antoine/.node-gyp/0.8.9/src/node_object_wrap.h, line 60.
Abort trap: 6

@graouts
Copy link
Author

graouts commented Sep 28, 2012

Interestingly, it seems notificationCenter has no methods on it. Calling .methods() on it returns an empty array.

@TooTallNate
Copy link

@graouts so the problem is with your observer object. It's a regular JavaScript object, but it needs to be a NSObject subclass. Your subclass will have to implement the "receivedNotificcation:" method. Take a look at https://github.com/TooTallNate/NodObjC/blob/master/test/extend.js for an example.

@graouts
Copy link
Author

graouts commented Sep 28, 2012

@TooTallNate Thanks for the info. I'm having some trouble working out what the parameters to .addMethod() should be. Is the first parameter a selector, ie. should it be receivedNotification:? Here's what I'm doing right now:

// define a NSObject subclass for our notification observer
var NotificationObserver = $.NSObject.extend('NotificationObserver');

// define the receivedNotification() method
NotificationObserver.addMethod('receivedNotification:', '@@:', function (self, _cmd) {
    console.log('received notification');
});

// register the class
NotificationObserver.register();

What about parameters, how do I let my method receive a parameter? Do I add a third @ in the second parameter and then name that parameter in the function definition, say like this?

// define a NSObject subclass for our notification observer
var NotificationObserver = $.NSObject.extend('NotificationObserver');

// define the receivedNotification() method
NotificationObserver.addMethod('receivedNotification:', '@@@:', function (self, _cmd, notification) {
    console.log('received notification', notification);
});

// register the class
NotificationObserver.register();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment