Code excerpt:
@protocol xpctestprotocol <NSObject>
- (void) put: (NSString *) aString replay: (void (^) (NSString * aString)) aReply;
@ end
@implementation AppDelegate
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification {
// Insert code here to initialize your application
NSXPCConnection * aConnection = [[NSXPCConnection alloc] initWithMachServiceName: @ "org.nursery-framework.xpctest" options: NSXPCConnectionPrivileged];
NSXPCInterface * anInterface = [NSXPCInterface interfaceWithProtocol: @protocol (xpctestprotocol)];
[aConnection setRemoteObjectInterface: anInterface];
[aConnection resume];
id <xpctestprotocol> aDaemon = [aConnection remoteObjectProxy];
[aDaemon put: @ "Hello" replay: ^ (NSString * aString) {
NSLog (@ "% @", aString);
[self performSelectorOnMainThread: @selector (updateUI :) withObject: aString waitUntilDone: NO];
}];
}
- (void) updateUI: (NSString *) aString
{
[[self textField] setStringValue: aString];
}
@ end
@implementation xpctestdaemon
- (BOOL) listener: (NSXPCListener *) listener shouldAcceptNewConnection: (NSXPCConnection *) newConnection
{
NSXPCInterface * anInterface = [NSXPCInterface interfaceWithProtocol: @protocol (xpctestprotocol)];
[newConnection setExportedInterface: anInterface];
[newConnection setExportedObject: self];
[newConnection resume];
return YES;
}
- (void) put: (NSString *) aString replay: (void (^) (NSString *)) aReply
{
aReply ([aString stringByAppendingString: aString]);
}
@ end
org.nursery-framework.xpctest.plist is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.nursery-framework.xpctest</string>
<key>Program</key>
<string>/Users/aki/Library/Developer/Xcode/DerivedData/xpctest-cpkkdfbfxfkomkfzkazhhmjxtoea/Build/Products/Debug/xpctest</string>
<key>MachServices</key>
<dict>
<key>org.nursery-framework.xpctest</key>
<true/>
</dict>
</dict>
</plist>