gpt4 book ai didi

macos - 使用 XPC 与另一个应用程序通信

转载 作者:行者123 更新时间:2023-12-03 16:02:14 29 4
gpt4 key购买 nike

我有一个窗口应用程序,为了添加一些功能,我需要另一个应用程序,该应用程序在登录时启动并将数据同步到服务器(如果可用)。

我尝试过 NSDistributionNotification,但它在沙盒应用程序中几乎没有用。我查找了 XPC 并希望它能够工作,但我只是不知道如何让它与助手一起工作。到目前为止,我已经使用 XPC 完成了这项工作。

主应用

    NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(AddProtocol)];
NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithServiceName:@"com.example.SampleService"];

xpcConnection.remoteObjectInterface = remoteInterface;

xpcConnection.interruptionHandler = ^{
NSLog(@"Connection Terminated");
};

xpcConnection.invalidationHandler = ^{
NSLog(@"Connection Invalidated");
};

[xpcConnection resume];

NSInteger num1 = [_number1Input.stringValue integerValue];
NSInteger num2 = [_number2Input.stringValue integerValue];

[xpcConnection.remoteObjectProxy add:num1 to:num2 reply:^(NSInteger result) {
NSLog(@"Result of %d + %d = %d", (int) num1, (int) num2, (int) result);
}];

XPC服务

In main () ...
SampleListener *delegate = [[SampleListener alloc] init];
NSXPCListener *listener = [NSXPCListener serviceListener];
listener.delegate = delegate;
[listener resume];

// In delegate
-(BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
NSXPCInterface *interface = [NSXPCInterface interfaceWithProtocol:@protocol(AddProtocol)];
newConnection.exportedInterface = interface;
newConnection.exportedObject = [[SampleObject alloc] init];
[newConnection resume];
return YES;
}

// In Exported Object class

-(void)add:(NSInteger)num1 to:(NSInteger)num2 reply:(void (^)(NSInteger))respondBack {
resultOfAddition = num1 + num2;
respondBack(resultOfAddition);
}

这工作正常,现在我需要将此结果传递给 Helper 应用程序。我怎样才能做到这一点 ?如果 XPC 不是此处进行通信的答案,那么我应该使用哪一个?请问有什么指点吗?

最佳答案

好吧,对于那些一直在努力解决这个问题的人来说,我终于能够使用 NSXPCConnection 100% 在两个应用程序进程之间进行通信

要注意的关键是,您只能为三件事创建 NSXPCConnection

  1. XPCService。您可以严格通过以下方式连接到 XPCService一个名字
  2. 马赫服务。您还可以连接到 Mach 服务严格通过名称
  3. 一个 NSXPCEndpoint。这就是我们寻找两个应用程序进程之间的通信。

问题是我们无法直接将 NSXPCEndpoint 从一个应用程序传输到另一个应用程序。

它涉及创建一个包含 NSXPCEndpoint 属性的 machservice Launch Agent ( See this example for how to do that )。一个应用程序可以连接到 machservice,并将该属性设置为其自己的 [NSXPCListener anonymousListener].endpoint

然后其他应用程序可以连接到 machservice,并请求该端点。

然后使用该端点,可以创建一个 NSXPCConnection,这成功地在两个应用程序之间建立了桥梁。我已经测试过来回发送对象,一切都按预期工作。

请注意,如果您的应用程序是沙盒的,您必须创建一个 XPCService,作为应用程序和 Machservice 之间的中间人

我对这项工作感到非常兴奋——我在 SO 方面相当活跃,所以如果有人对源代码感兴趣,只需添加评论,我就可以努力发布更多详细信息

我遇到的一些障碍:

您必须启动您的 machservice,这些是:

   OSStatus                    err;
AuthorizationExternalForm extForm;

err = AuthorizationCreate(NULL, NULL, 0, &self->_authRef);
if (err == errAuthorizationSuccess) {
NSLog(@"SUCCESS AUTHORIZING DAEMON");
}
assert(err == errAuthorizationSuccess);

Boolean success;
CFErrorRef error;

success = SMJobBless(
kSMDomainSystemLaunchd,
CFSTR("DAEMON IDENTIFIER HERE"),
self->_authRef,
&error
);

此外,每次重建守护进程时,都必须使用以下 bash 命令卸载以前的启动代理:

sudo launchctl unload /Library/LaunchDaemons/com.example.apple-samplecode.EBAS.HelperTool.plist
sudo rm /Library/LaunchDaemons/com.example.apple-samplecode.EBAS.HelperTool.plist
sudo rm /Library/PrivilegedHelperTools/com.example.apple-samplecode.EBAS.HelperTool

(当然还有您相应的标识符)

关于macos - 使用 XPC 与另一个应用程序通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24040765/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com