CPDistributedMessagingCenter

From iPhone Development Wiki
Revision as of 22:36, 21 September 2009 by Dustin Howett (talk | contribs) (Created page with '{{occlass|library=AppSupport.framework}} CPDistributedMessagingCenter is a simple wrapper over the existing messaging facilities in the operating system. It provides two-way…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


CPDistributedMessagingCenter is a simple wrapper over the existing messaging facilities in the operating system. It provides two-way communication between different processes using simple messages and dictionaries. All dictionaries transferred must be easily flattenable or contain only simple objects.

Server

CPDistributedMessagingCenter *messagingCenter;
// Center name must be unique, recommend using application identifier.
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];
[messagingCenter runServerOnCurrentThread];

// Register Messages
[messagingCenter registerForMessageName:@"messageThatHasInfo" target:self selector:@selector(handleMessageNamed:withUserInfo:)];
[messagingCenter registerForMessageName:@"message" target:self selector:@selector(handleSimpleMessageNamed:)];

- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo {
    // ...
}

- (void)handleSimpleMessageNamed:(NSString *)name {
    // ...
}

Client

CPDistributedMessagingCenter *messagingCenter;
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];

// One-way (message only)
[messagingCenter sendMessageName:@"message" userInfo:nil];

// Two-way (wait for reply)
NSDictionary *reply;
reply = [messagingCenter sendMessageAndReceiveReplyName:@"messageThatHasInfo" userInfo:/* dictionary */];

Header