Finder Sync Extension和主应用程序应如何通信?

mix*_*y87 5 synchronization xpc nsnotifications findersync

我的用例:我有一个'MainApp'来执行文件的同步.我希望'MainApp'处理有关同步和其他REST API调用的所有服务器调用,例如文档共享等.

另一方面,我会有一个Finder Sync Extension,它会显示同步状态图标叠加.它还有一个文件上下文菜单项"共享",它将显示一个"共享"对话框,用户可以选择与谁共享文件.

问题:

  1. FinderSyncExtension和MainApp应如何通信?应该使用XCP,如果是,那么通信是双向的吗?例如,MainApp通知Finder它应该刷新,因为一些文件已经同步,Finder通知MainApp它应该执行"共享"操作.

  2. 谁应该出现"分享"对话框?单击FinderSyncExtension"共享"菜单项时,应显示共享表单.这应该由查找程序扩展程序还是由MainApp显示(假设FinderExtension通知它"点击"共享项目).

如果Finder扩展应该显示表单,那么FinderExtension还应该从服务器检索数据(例如联系人和组以进行共享),我不确定Finder Extension是否应该对服务器执行任何网络调用.

研究这个主题,我找到了几种方法:

  1. FinderSyncExtension和MainApp不直接通信.相反,FinderExtension从数据库中读取数据以正确显示徽章.在这种情况下,不清楚FinderExtension在同步完成时应该如何更新,或者它应该如何通知MainApp执行某些操作.
  2. XPC通信.我猜FinderExtension可以启动对MainApp的调用,但预期方向相反?
  3. macOS进程之间是否存在某种NotificationCenter?我试着用NSWorkspace.sharedWorkspace.notificationCenterNSDistributedNotificationCenter.defaultCenter,但他们似乎并不在MainApp交付通知.
  4. 在Seafile项目中的mach_ports?

mix*_*y87 5

我设法通过CFMessagePortAPI 做到这一点.为了使沙盒扩展和主应用程序进行通信,需要在Xcode功能中启用AppGroup.此外,需要使用带后缀的app-group键(按您的选择)作为消息端口标识符.

主要应用

在主应用程序的某个地方,这种代码会监听消息端口:

CFMessagePortRef port = CFMessagePortCreateLocal(nil, CFSTR("group.com.yourapp.mach_or_something"), Callback, nil,
                                                 nil);
CFRunLoopSourceRef runLoopSource = CFMessagePortCreateRunLoopSource(nil, port, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
Run Code Online (Sandbox Code Playgroud)

Callback 是一种实现方法:

static CFDataRef Callback(CFMessagePortRef port, SInt32 messageID, CFDataRef data, void* info)
{
    NSData* objcData = (__bridge NSData*) data;
    NSLog(@"Message received: %@", [NSString.alloc initWithData:objcData encoding:NSASCIIStringEncoding]);
    return data;
}
Run Code Online (Sandbox Code Playgroud)

Finder同步扩展

然后,在扩展中的某个地方(即当用户点击菜单项时):

CFDataRef data = CFDataCreate(NULL, (const UInt8*) "somedata", 8);
SInt32 messageID = 0x1111; // Arbitrary
CFTimeInterval timeout = 1;

CFMessagePortRef remotePort = CFMessagePortCreateRemote(nil, CFSTR("group.com.yourapp.mach_or_something"));

SInt32 status = CFMessagePortSendRequest(remotePort, messageID, data, timeout, timeout, NULL, NULL);
if (status == kCFMessagePortSuccess)
{
    NSLog(@"SUCCESS STATUS");
}
else
{
    NSLog(@"FAIL STATUS");
}
Run Code Online (Sandbox Code Playgroud)

这将向主应用程序发送消息.

  • 我不认为这是Sierra的一个问题.我将你的回调函数修改为类似[this](https://gist.github.com/hkalexling/1358f9bb2e0708b7bc1ba03daaeb97d0),然后它在Sierra工作没有任何问题 (3认同)