2011-09-30 4 views
2

Je dois communiquer entre deux applications de console différentes, Observer et Client.Cocoa NSNotificationCenter la communication entre les applications a échoué

Dans l'application Observer j'ai ajouté ce code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self]; 

Dans l'application Client j'ai ajouté ce 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]); 
} 

, mais rien ne se passe. J'ai lu toute la documentation, mais je suis flambant neuf sous Mac OS X. Des idées?


Je lis sur les notifications distribuées, j'ai changé mon code et ressemble maintenant à ceci: Du côté du serveur:

[[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]); 
} 

Dans le côté client:

NSMutableDictionary *info; 
info = [NSMutableDictionary dictionary]; 
[info setObject:@"foo" forKey:@"bar"]; 

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification" 
            object:nil 
           userInfo:info 
         deliverImmediately:YES]; 

I exécuter les deux applications, mais rien ne se passe. Qu'est-ce que je fais mal?

Répondre

0

J'ai résolu mon problème. Voici le code source: Dans le côté client:

NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys: @"John Doe", @"name", @"22222222222", @"phone", @"Dummy address", @"address", nil]; 

//Post the notification 
NSString *observedObject = @"com.test.net"; 
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter]; 
[center postNotificationName: @"Plugin Notification" object: observedObject userInfo: user deliverImmediately: YES]; 

Dans le côté serveur

NSString *observedObject = @"com.test.net"; 
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter]; 
[center addObserver: self selector: @selector(receiveNotification:) name: @"Plugin Notification" object: observedObject]; 

receiveNotification définition

-(void)receiveNotification:(NSNotification*)notif 
{ 
    NSlog(@"Hello"); 
} 

Dans la méthode dealloc

[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name: @"Plugin Notification" object: nil]; 
Questions connexes