gpt4 book ai didi

macos - 如何从 XPC 助手应用程序向主应用程序发送消息?

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

我成功创建了 XPC 服务并通过从主应用程序发送消息与 XPC 服务进行通信。但我想知道的是,是否可以发起从XPC服务到主应用程序的通信。 Apple documentation说XPC是双向的。如果有人能用一个例子为我指明正确的方向,我将不胜感激。

请注意,

  • 我想从主应用程序启动 XPC。
  • 从主应用程序与 XPC 通信。
  • 当某些事件发生时,XPC 应向主应用程序发送消息。

我在前两个中成功了,但在第三个中找不到任何资源。

谢谢。 :)

最佳答案

一切都弄清楚了。下面应该是一个不错的例子:

ProcessorListener.h(包含在客户端和服务器中):

@protocol Processor

- (void) doProcessing: (void (^)(NSString *response))reply;

@end

@protocol Progress

- (void) updateProgress: (double) currentProgress;
- (void) finished;

@end

@interface ProcessorListener : NSObject<NSXPCListenerDelegate, Processor>

@property (weak) NSXPCConnection *xpcConnection;

@end

ProcessorListener.m:(仅包含在服务器中)

#import "ProcessorListener.h"

@implementation ProcessorListener

- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
{
[newConnection setExportedInterface: [NSXPCInterface interfaceWithProtocol:@protocol(Processor)]];
[newConnection setExportedObject: self];
self.xpcConnection = newConnection;

newConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol: @protocol(Progress)];

// connections start suspended by default, so resume and start receiving them
[newConnection resume];

return YES;
}

- (void) doProcessing: (void (^)(NSString *g))reply
{
dispatch_async(dispatch_get_global_queue(0,0), ^{
for(int index = 0; index < 60; ++index)
{
[NSThread sleepWithTimeInterval: 1];
[[self.xpcConnection remoteObjectProxy] updateProgress: (double)index / (double)60 * 100];
}

[[self.xpcConnection remoteObjectProxy] finished];
}

// nil is a valid return value.
reply(@"This is a reply!");
}

@end

MainApplication.m(您的主应用程序):

#import "ProcessorListener.h"

- (void) executeRemoteProcess
{
// Create our connection
NSXPCInterface * myCookieInterface = [NSXPCInterface interfaceWithProtocol: @protocol(Processor)];

NSXPCConnection * connection = [[NSXPCConnection alloc] initWithServiceName: kServiceName];

[connection setRemoteObjectInterface: myCookieInterface];

connection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(Progress)];
connection.exportedObject = self;

[connection resume];

id<Processor> theProcessor = [connection remoteObjectProxyWithErrorHandler:^(NSError *err)
{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: @"OK"];
[alert setMessageText: err.localizedDescription];
[alert setAlertStyle: NSWarningAlertStyle];

[alert performSelectorOnMainThread: @selector(runModal) withObject: nil waitUntilDone: YES];
}];

[theProcessor doProcessing: ^(NSString * response)
{
NSLog(@"Received response: %@", response);
}];
}

#pragma mark -
#pragma mark Progress

- (void) updateProgress: (double) currentProgress
{
NSLog(@"In progress: %f", currentProgress);
}

- (void) finished
{
NSLog(@"Has finished!");
}

@end

请注意,此代码是基于我的工作代码的精简版本。它可能无法 100% 编译,但显示了所使用的关键概念。在示例中,doProcessing运行异步以显示 Progress 中定义的回调即使在初始方法返回并且Received response: This is a reply!之后,协议(protocol)仍然会被执行。已记录。

关于macos - 如何从 XPC 助手应用程序向主应用程序发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623565/

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