XPC 在工作区中的两个 cocoa 应用程序之间,NSXPCConnection 立即失效
XPC Between two cocoa applications in workspace, the NSXPCConnection is immediately being invalidated
我有两个 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);
}];
这是我的接收器应用程序的应用程序委托:
@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
:
Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service
named com.TechSmith.TestXPCHelper was invalidated." UserInfo={NSDebugDescription=The
connection to service named com.TechSmith.TestXPCHelper was invalidated.}
所以我认为我在实例化 NSXPCConnection
时做错了什么。我找不到两个应用程序互相对话的好例子——它总是一个应用程序和一项服务。那是我的问题吗?我需要在应用程序对话之间提供服务吗?
是否有任何方法可以获取有关此连接为何失效的更多信息?这也有很大帮助
这里的问题非常简单,
原来 initWithMachServiceName
正在明确寻找 mach 服务。我正在使用另一个应用程序进程的标识符。
如果我实际使用有效 mach 服务的标识符,则没有问题
请注意,还有其他两种方法可以创建 NSXPCConnection
、
带有 NSXPCEndpoint
或 XPCService
标识符
我有两个 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);
}];
这是我的接收器应用程序的应用程序委托:
@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
:
Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.TechSmith.TestXPCHelper was invalidated." UserInfo={NSDebugDescription=The connection to service named com.TechSmith.TestXPCHelper was invalidated.}
所以我认为我在实例化 NSXPCConnection
时做错了什么。我找不到两个应用程序互相对话的好例子——它总是一个应用程序和一项服务。那是我的问题吗?我需要在应用程序对话之间提供服务吗?
是否有任何方法可以获取有关此连接为何失效的更多信息?这也有很大帮助
这里的问题非常简单,
原来 initWithMachServiceName
正在明确寻找 mach 服务。我正在使用另一个应用程序进程的标识符。
如果我实际使用有效 mach 服务的标识符,则没有问题
请注意,还有其他两种方法可以创建 NSXPCConnection
、
带有 NSXPCEndpoint
或 XPCService
标识符