I need to communicate between two different console apps, Observer and Client.
In the Observer app I added this code:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
In the Client app I added this code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
but nothing happens. I read all the documentation, but I am brand new in Mac OS X.
Any ideas?
I read about the Distributed Notifications, I changed my code and now looks like this:
In the server side:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
In the client side:
NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil
userInfo:info
deliverImmediately:YES];
I run both applications, but nothing happens. What am I doing wrong?
I solved my problem. This is the source code:
In the client side:
In the server side
receiveNotification definition
In dealloc method