Created
September 28, 2012 09:16
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'"); |
@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
@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.