-
-
Save graouts/3798828 to your computer and use it in GitHub Desktop.
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'"); |
Interestingly, it seems notificationCenter
has no methods on it. Calling .methods()
on it returns an empty array.
@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.
@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();
Running this code results in the following error: