LoginSignup
1
1

More than 5 years have passed since last update.

[OSX]プロセス間通信(Distributed Notifications)

Posted at

Mach由来だと思われるDistributed Notificationsについて調べてみた。OSXでは異なるアプリケーションに対して通知を送る事ができる。

通知を受け取る側(DistributedServer)のコードは以下のとおり。

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog(@"%s", __func__);
    [self _registerForNotes];
}
 
- (void)_registerForNotes
{
    NSLog(@"%s", __func__);
    NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
    [dnc addObserver:self
            selector:@selector(_handleDistributedNote:)
                name:@"DemoDistributedNote"
              object:nil];
}
 
- (void)_handleDistributedNote:(NSNotification *)note
{
    NSLog(@"%s Recieived Distributed Notification!:%@", __func__, note);
}
@end

通知を送る側(DistributedClient)のコードは以下のとおり。

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog(@"%s", __func__);
    [self _postNotes];
}
 
- (void)_postNotes
{
    NSLog(@"%s", __func__);
    NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
    [dnc postNotificationName:@"DemoDistributedNote"
                       object:nil];
}
@end

DistributedServerを起動した後に、DistributedClientを起動すると、アプリケーション間で通知が送られることが確認できる。

ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/mac/DistributedServer - GitHub

https://github.com/murakami/workbook/tree/master/mac/DistributedClient - GitHub

関連情報
Cocoa in a Nutshell

【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1