从OSX上的App-A到App-B的NSXPCConnection未启动连接

时间:2019-05-17 09:30:24

标签: objective-c macos nsxpcconnection

App-A需要与嵌入在App-B内部的XPCService通信。

使用以下代码从App-A创建NSXPCConection

// Resuming the connection

 NSXPCInterface *myCookieInterface = [NSXPCInterface interfaceWithProtocol:@protocol(FeedMeACookie)];
 self.serviceConnection =    [[NSXPCConnection alloc]initWithServiceName:@"com.mycompany.HService"];
 [self.serviceConnection setInvalidationHandler:^{
                NSLog(@"Bridgeagent invalidation handler!");
     }];

  [self.serviceConnection setInterruptionHandler:^{
                NSLog(@"Bridgeagent interruption handler!");
     }];
  self.serviceConnection.remoteObjectInterface = myCookieInterface;
  [self.serviceConnection resume];

 //Starting Communication

   [[self.serviceConnection remoteObjectProxy] feedMeACookie:@"Hello"];

XPCService在App-B中,

-(BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
    // This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
    NSLog(@"shouldAcceptNewConnection");
    // Configure the connection.
    // First, set the interface that the exported object implements.
    newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(HServiceProtocol)];

    // Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object.
    HService *exportedObject = [HService new];
    newConnection.exportedObject = exportedObject;

    // Resuming the connection allows the system to deliver more incoming messages.
    [newConnection resume];

    // Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
    return YES;
}
int main(int argc, const char *argv[])
{
    // Create the delegate for the service.
    ServiceDelegate *delegate = [ServiceDelegate new];

    // Set up the one NSXPCListener for this service. It will handle all incoming connections.
    NSXPCListener *listener = [NSXPCListener serviceListener];
    listener.delegate = delegate;

    // Resuming the serviceListener starts this service. This method does not return.
    [listener resume];
    return 0;
}

//Protocol Method
    - (void)feedMeACookie: (NSString *)cookie{
        NSLog(@"=======> feedMeACookie");
    }

它无法在App-B中从App-A调用XPCService

是否可以从App-A调用App-B XPCService,它完全是一个独立的应用程序,如果不是,那么在Mac OS上实现这两个应用程序之间这种通信的替代方法是什么?

0 个答案:

没有答案