XPC在工作空间中的两个cocoa应用程序之间,NSXPCConnection立即失效

时间:2017-01-27 19:23:22

标签: macos cocoa xpc nsxpcconnection

我有两个Cocoa应用程序,一个将成为发送者,另一个将成为此XPC关系中的接收者。

在发件人的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);
  }];

这是我的接收器应用程序的app委托:

@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代码= 4099“与服务的连接   名为com.TechSmith.TestXPCHelper的名字无效。“UserInfo = {NSDebugDescription = The   与名为com.TechSmith.TestXPCHelper的服务的连接无效。}

所以我认为我对NSXPCConnection的实例化做错了。我找不到两个应用程序相互交谈的好例子 - 它总是一个应用程序和一个服务。那是我的问题吗?我需要在应用程序之间进行服务吗?

有没有办法获得有关此连接失效原因的更多信息?这也有很大帮助

1 个答案:

答案 0 :(得分:3)

这里非常直接的问题, 结果initWithMachServiceName明确地寻找一个马赫服务。我正在使用另一个申请流程的标识符。

如果我实际使用有效马赫服务的标识符,则没有问题

请注意,还有另外两种方法可以创建NSXPCConnection

带有NSXPCEndpointXPCService标识符