MjZ*_*Zac 15 macos cocoa xpc nsxpcconnection
我通过从主应用程序发送消息成功创建了XPC服务并与XPC服务进行通信.但我想知道的是,它是否可以启动从XPC服务到主应用程序的通信.在苹果的文档说,XPC是双向的.如果有人可以通过一个例子指出我正确的方向,将不胜感激.
请注意,
我在前两个成功了,但在第三个上找不到任何资源.
谢谢.:)
Kyl*_*yle 15
想出一切.以下应该是一个不错的例子:
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
请注意,此代码是基于我的工作代码的精简版本.它可能无法100%编译,但显示了使用的关键概念.在该示例中,doProcessing
运行异步以显示Progress
即使在初始方法返回并且Received response: This is a reply!
已记录之后,协议中定义的回调仍然执行.
归档时间: |
|
查看次数: |
6686 次 |
最近记录: |