У меня есть два приложения Cocoa, один из них будет отправителем, а другой - приемником в этом соотношении XPC.XPC Между двумя приложениями какао в рабочем пространстве, NSXPCConnection немедленно недействителен
В applicationDidFinishLaunching
в отправителе, я сначала открыть второе приложение приемника
NSError* error = nil;
NSURL* url = [[NSBundle mainBundle] bundleURL];
url = [url URLByAppendingPathComponent:@"Contents" isDirectory:YES];
url = [url URLByAppendingPathComponent:@"MacOS" isDirectory:YES];
url = [url URLByAppendingPathComponent:@"TestXPCHelper.app" isDirectory:YES];
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:url
options:NSWorkspaceLaunchWithoutActivation
configuration:[NSDictionary dictionary]
error:&error];
if (error)
{
NSLog(@"launchApplicationAtURL:%@ error = %@", url, error);
[[NSAlert alertWithError:error] runModal];
}
Затем я создаю мой NSXPCConnection
assert([NSThread isMainThread]);
if (self.testConnection == nil) {
self.testConnection = [[NSXPCConnection alloc] initWithMachServiceName:NEVER_TRANSLATE(@"com.TechSmith.TestXPCHelper") options:NSXPCConnectionPrivileged];
self.testConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(TestXPCProtocol)];
self.testConnection.interruptionHandler = ^{
NSLog(@"Connection Terminated");
};
self.testConnection.invalidationHandler = ^{
self.testConnection.invalidationHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.testConnection = nil;
}];
};
[self.testConnection resume];
}
Тогда я пытаюсь отправить сообщение через соединение (соединение уже утратила силу здесь)
id<TestXPCProtocol> testRemoteObject= [self.testConnection remoteObjectProxy];
[testRemoteObject testXPCMethod2];
[[self.testConnection remoteObjectProxyWithErrorHandler:^(NSError * proxyError){
NSLog(@"%@", proxyError);
}] testXPCMethod:^(NSString* reply) {
NSLog(@"%@", reply);
}];
А вот приложение делегата для моего приложения приемника:
@interface AppDelegate() <NSXPCListenerDelegate, TestXPCProtocol>
@property (weak) IBOutlet NSWindow *window;
@property NSXPCListener *xpcListener;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSLog(@"TESTING123");
self.xpcListener = [[NSXPCListener alloc] initWithMachServiceName:@"com.TechSmith.TestXPCHelper"];
self.xpcListener.delegate = self;
[self.xpcListener resume];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
NSLog(@"ACTIVE234");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (void)run
{
NSLog(@"RUNNING");
// Tell the XPC listener to start processing requests.
[self.xpcListener resume];
// Run the run loop forever.
[[NSRunLoop currentRunLoop] run];
}
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
{
NSLog(@"LISTENING");
assert(listener == self.xpcListener);
#pragma unused(listener)
assert(newConnection != nil);
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(TestXPCProtocol)];
newConnection.exportedObject = self;
[newConnection resume];
return YES;
}
- (void)testXPCMethod:(void(^)(NSString * version))reply
{
NSLog(@"HEY");
reply(@"REPLY HERE");
}
- (void)testXPCMethod2
{
NSLog(@"TWO!");
}
Вот proxyError
, когда я пытаюсь отправить сообщение через соединение:
Ошибка домена = NSCocoaErrorDomain Code = 4099 «Связь для обслуживания с именем com.TechSmith.TestXPCHelper был признан недействительным ». UserInfo = {NSDebugDescription = подключения к услуге имени com.TechSmith.TestXPCHelper был признан недействительным.}
Так что я думаю, что я делаю что-то не так с моей инстанциацией NSXPCConnection
. Я не могу найти хороший пример двух приложений, говорящих друг другу - это всегда одно приложение и сервис. Это моя проблема? Мне нужна услуга между приложениями, говорящими?
Есть ли способ получить дополнительную информацию о том, почему это соединение недействительно? Это также помогло бы многим